$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom shortcode being executed when saving page in wp-admin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom shortcode being executed when saving page in wp-admin

matteradmin9PV0评论

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?

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 edited Apr 9, 2014 at 5:48 Matt Stone asked Apr 7, 2014 at 4:57 Matt StoneMatt Stone 2102 silver badges6 bronze badges 10
  • Why are you exiting in the middle of a shortcode..? That's very wrong. Probably where your issues are coming from. – Dan Commented Apr 7, 2014 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 Commented Apr 7, 2014 at 5:23
  • 2 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 Commented Apr 7, 2014 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 Commented Apr 7, 2014 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 Commented Apr 7, 2014 at 12:07
 |  Show 5 more comments

2 Answers 2

Reset to default 10

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.

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.

Post a comment

comment list (0)

  1. No comments so far