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.
Welcome.
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.
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!';
}
);
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();
}