1

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!

3
  • what is "echo_ald_crp()" does? Commented Jul 28, 2016 at 20:57
  • It's a plugin that displays related blog posts based off of the page title. Commented Jul 28, 2016 at 21:05
  • If you're adding this to the end of every single posts content, you would be better off adding it to your theme template instead of as a shortcode Commented Jul 29, 2016 at 10:05

2 Answers 2

3

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

8
  • Thanks! I tried adding the shortcode [shortcode_ald_crp] but it just displays that text rather than the blog posts. I added everything you said to the functions. What would cause that? Thanks! Commented Jul 28, 2016 at 21:09
  • are you sure that echo_ald_crp() return the whole blog post? Because it is suppose to alter the "output". Commented Jul 28, 2016 at 21:13
  • Yes. I am currently using the PHP script on my product page template, and it's showing the related blog posts. Commented Jul 28, 2016 at 21:16
  • I added this to my functions: //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(); } Commented Jul 28, 2016 at 21:22
  • So all you see is [shortcode_ald_crp] ? And this code is in functions.php of your active theme? Commented Jul 28, 2016 at 21:32
1

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.

3
  • It's probably better to use ob_get_clean() - php.net/manual/en/function.ob-get-clean.php - ob_get_contents doesn't clear the output buffer. Commented Jul 29, 2016 at 2:45
  • @jgraup Great catch, I've updated the answer. Commented Jul 29, 2016 at 9:27
  • While you're at it, maybe wrap it all in the conditional. No need to run excess functions if nothing will yield ;) Commented Jul 29, 2016 at 13:26

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.