I need to turn this code:
<?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
into a shortcode. Can someone help me do this? I have researched but am just lost to be honest. Thanks!
I need to turn this code:
<?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
into a shortcode. Can someone help me do this? I have researched but am just lost to be honest. Thanks!
Init your shortcode
add_shortcode('shortcode_ald_crp', 'myshortcode_echo_ald_crp');
The function what you want:
function myshortcode_echo_ald_crp() {
ob_start();
if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp();
return ob_get_clean();
}
you call you shortcode in a post like this:
[shortcode_ald_crp]
Or into the php code:
echo do_shortcode('[shortcode_ald_crp]');
UPDATE
Change the function add_shortcode
shortcode_ald_crp for myshortcode_echo_ald_crp
that echo_ald_crp() return the whole blog post? Because it is suppose to alter the "output".
//blog posts add_shortcode('shortcode_ald_crp', 'myshortcode_echo_ald_crp'); function shortcode_ald_crp() { ob_start(); if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); return ob_get_clean(); }
[shortcode_ald_crp] ? And this code is in functions.php of your active theme?
Do you mean something like this (untested):
// function for your shortcode
function shortcode_action($atts) {
ob_start();
if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp();
$content = ob_get_clean();
return $content;
}
// creates shortcode [shortcodehandle] so change it accordingly
add_shortcode( 'shortcodehandle', 'shortcode_action' );
Update: using ob_get_contents to return the content of the output of echo_ald_crp.
Update 2: using ob_get_clean() as highlighted by @jgraup in the comments.