Shortcode not being executed

admin2025-01-07  5

I place the following code within the WP 'init' callback (or when plugins are loaded).

add_shortcode('my_shortcode',
               function($atts, $content ='') { die(); }
             );
if (!shortcode_exists('my_shortcode')) die();

In my page I put "[my_shortcode]"

When I view the page I get "****"

Any idea what happened to my code?


Update: I have simplified the problem.

I added the shortcode definition in my theme's index.php file.

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: 
 *
 * @package GeneratePress
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

add_shortcode('myt_active_plugins',
             function($atts, $content, $name) {
                    return 'Shortcode injected';
                  }
            );

I deactivated all plugins.

I (re)installed WP3.5.2

I created a post:

Welcome *[myt_active_plugins]*

I published the post and when I viewed it I got:

Welcome **

As a final check I installed a shortcode plugin, (Shortcodes Ultimate) and it acted the same.

I place the following code within the WP 'init' callback (or when plugins are loaded).

add_shortcode('my_shortcode',
               function($atts, $content ='') { die(); }
             );
if (!shortcode_exists('my_shortcode')) die();

In my page I put "[my_shortcode]"

When I view the page I get "****"

Any idea what happened to my code?


Update: I have simplified the problem.

I added the shortcode definition in my theme's index.php file.

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package GeneratePress
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

add_shortcode('myt_active_plugins',
             function($atts, $content, $name) {
                    return 'Shortcode injected';
                  }
            );

I deactivated all plugins.

I (re)installed WP3.5.2

I created a post:

Welcome *[myt_active_plugins]*

I published the post and when I viewed it I got:

Welcome **

As a final check I installed a shortcode plugin, (Shortcodes Ultimate) and it acted the same.

Share Improve this question edited Jan 4, 2020 at 11:08 Karl Bochert asked Jan 3, 2020 at 17:51 Karl BochertKarl Bochert 12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Welcome.

  1. By using die function, You are stopping the process and it will stop immediately after running the shortcode callback. Better to let the rest of the code to be executed.

  2. You need to return something in shortcode callback function instead of sending headers using die or echo.

So, change your code to something like the following one and it should work. You are free to wrap it in init hook.

add_shortcode(
    'my_shortcode',
    function($atts, $content ='') {
        return 'This is from My shortcode!';
    }
);
  1. The condition for checking shortcode existence is problematic. If you want to check if a certain shortcode exists in a page, has_shortcode function. Check has_shortcode document here.

You can try that.

add_shortcode('my_shortcode', 'my_shortcode_function');
function my_shortcode_function( $content ) 
{
    ob_start();
    // do something to $content
    echo $content;
    return ob_get_clean();
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265473a1048.html

最新回复(0)