Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a custom shortcode:

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

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:\xampp\htdocs\126-indooroopilly-shopping-centre\www\public_html\wp-content\themes\indooroopilly\include\shortcodes.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?

share|improve this question
    
Why are you exiting in the middle of a shortcode..? That's very wrong. Probably where your issues are coming from. –  Dan Apr 7 at 5:17
    
This is just a demonstration of the issue - I wouldn't expect the echo and exit to actually be executed when saving the page, but it is. I can post the real shortcode if you like, but it's pretty project specific. –  Matt Stone Apr 7 at 5:23
1  
Does this still happen, after you disabled all plugins and switched to one of the Twenty* themes? Please follow the linked process to identify the conflicting plugin or theme. –  kaiser Apr 7 at 6:52
    
@kaiser Good idea to refer the OP to the flow chart. It's a cool flow chart but it took me some time to figure out where to start ;-) This version shows the starting point. –  birgire Apr 7 at 8:11
1  
"* I wouldn't expect the echo and exit to actually be executed when saving the page, but it is.*" vs Codex: "Producing the output directly will lead to unexpected results." –  Chip Bennett Apr 7 at 12:07
show 4 more comments

1 Answer 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.

share|improve this answer
    
I've added the original shortcode source to the question. It fails on a function call to another plugin, which hasn't been loaded in wp-admin. For me this boils down to - why is Wordpress trying to execute the shortcode when saving a page? For what reason? –  Matt Stone Apr 10 at 0:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.