0

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.

2 Answers 2

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.
10
  • Hector: Tried that. The die() instructions are just my debugging technique of last resort, used to verify that the program execution is or is not reaching that point. The first die() not being executed proves that WP thinks I have created the shortcut correctly. The second die() not being executed proves that my callback code was not being invoked. But some how WP still replaces the shortcode! Jan 3, 2020 at 20:21
  • You can enable WP_DEBUG and WP_DEBUG_LOG and then use error_log('something'); instead of die. BTW, does it work?
    – Hector
    Jan 3, 2020 at 20:24
  • Mohammed: Thats how I originally tried it, but my code was in as class and in a namespace, so I returned to the simpler way using the closure. In all cases wp reports that the shortcode exists but fails to actuall call the callback. Jan 3, 2020 at 20:25
  • Hector: I just tried WP_DEBUG ad it reported several unrelated errors which I fixed. The problem remains. I Added the logging you suggested but don't know where to find the resulting log. In any event, if the die() code were being reached, the resulting termination of the program would surely result in not displaying the page (which I'd notice!) Jan 3, 2020 at 20:36
  • 1
    Please edit your question and paste the class or related codes.
    – Hector
    Jan 4, 2020 at 7:27
0

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();
}
9
  • Same problem. Basically WP sees my shortcode as valid and replaces it with an empty string, but does not execute the callback! Jan 3, 2020 at 21:37
  • What are you passing in the function parameters?
    – Ahmedhere
    Jan 3, 2020 at 21:44
  • My code is just as above. add_shortcode is called with a string, and a callback function. add_shortcode('my-shortcode', my-callback); Jan 3, 2020 at 21:49
  • Then you may try this one code add_shortcode('my_shortcode', 'my_shortcode_function'); function my_shortcode_function( $content ) { ob_start(); // do something to $content echo $content; return ob_get_clean(); }
    – Ahmedhere
    Jan 3, 2020 at 21:51
  • Whatever I put in my_shortcode_function() never gets executed. Even a die(); instruction has no effect. WP is recognizing the shortcode and replacing it without ever calling my callback function. Jan 3, 2020 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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