Custom shortcode being executed when saving page in wp-admin

I’m working on a custom shortcode:

[abuzz-store slug="woolworths" fields="description,level,phone" more="true"]

Read More

The shortcode is working correctly on the website frontend. However, I can no longer save edits to pages in wp-admin. Various PHP errors are thrown from trying to execute the shortcode outside of the proper template.

Here is the shortcode functionality:

/**
 * [indo_store_details_shortcode description]
 *
 * @param  [type] $atts [description]
 * @return [type]       [description]
 */
function indo_store_details_shortcode($atts)
{
    $options = shortcode_atts(array(
        // Abuzz Store slug
        'slug'      => '',

        // Abuzz Store fields to display (title, description, level, phone)
        'fields'    => '',

        // Display a link to further store details (store page)
        'more'      => false,
    ), $atts);

    // sanity checks
    if (!$options['slug']) return '';
    // if (is_admin()) return '';

    $output = '';

    // retrieve store information
    $store = get_posts(array(
        'name' => $options['slug'],
        'post_type' => 'store',
    ))[0];

    // determine fields to display
    $fields = explode(',', $options['fields']);

    $output .= '<article class="abuzz-store">';

    // title field
    if (in_array('title', $fields)) {
        $output .= sprintf('<h2>%s</h2>', $post->post_title);
    }

    // "metadata" .options-list
    if (in_array('level', $fields) ||
        in_array('phone', $fields)) {

        $output .= '<ul class="nav  options-list">';

        // level field
        if (in_array('level', $fields)) {
            $levels = get_group('Level', $store->ID);

            $output .= sprintf('<li class="icon-text"><i class="icon-text__icon  icon  icon-level-black"></i> %s</li>', indo_combine_levels($levels));
        }

        // phone field
        if (in_array('phone', $fields)) {
            $output .= sprintf('<li class="icon-text"><i class="icon-text__icon  icon  icon-phone-black"></i> %s</li>', get('phone_number', 1, 1, $store->ID));
        }

        $output .= '</ul>';

    }

    // description field
    if (in_array('description', $fields)) {
        $output .= get('information_text', 1, 1, $store->ID);
    }

    // "More details" link
    if ($options['more']) {
        $output .= sprintf('<p><a href="%s" title="More details" class="btn  btn--black-arrow  icon-text--rev">More details  <i class="icon-text__icon  icon  icon-arrow-white"></i></a></p>', get_permalink($store->ID));
    }

    $output .= "</article>n";

    return $output;
}
add_shortcode('abuzz-store', 'indo_store_details_shortcode');

Error: Fatal error: Call to undefined function get_group() in D:xampphtdocs126-indooroopilly-shopping-centrewwwpublic_htmlwp-contentthemesindooroopillyincludeshortcodes.php on line 90

get_group is a function added by a 3rd party plugin (Magic Fields), which isn’t loaded in wp-admin. Which is what I’d expect.

Why is this being executed when the page is saved?

Related posts

Leave a Reply

2 comments

  1. Shortcodes must return, not echo or print their output. As the Codex entry for add_shortcode() explains:

    Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

    Change your shortcode callback function to return its value.

  2. You could also consider using PHP’s output buffering for your shortcode ob_start and ob_get_clean like this:

    function indo_store_details_shortcode( $attr ) {
      ob_start();
    
      //Add all your shortcode stuff here
    
      return ob_get_clean();
    }
    add_shortcode( 'abuzz-store', 'indo_store_details_shortcode' );
    

    The ob_start function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.