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!

  • what is "echo_ald_crp()" does? – Jean-philippe Emond Jul 28 '16 at 20:57
  • It's a plugin that displays related blog posts based off of the page title. – Brian Schnurr Jul 28 '16 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 – Tom J Nowell Jul 29 '16 at 10:05
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

| improve this answer | |
  • 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! – Brian Schnurr Jul 28 '16 at 21:09
  • are you sure that echo_ald_crp() return the whole blog post? Because it is suppose to alter the "output". – Jean-philippe Emond Jul 28 '16 at 21:13
  • Yes. I am currently using the PHP script on my product page template, and it's showing the related blog posts. – Brian Schnurr Jul 28 '16 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(); } – Brian Schnurr Jul 28 '16 at 21:22
  • So all you see is [shortcode_ald_crp] ? And this code is in functions.php of your active theme? – czerspalace Jul 28 '16 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.

| improve this answer | |
  • 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. – jgraup Jul 29 '16 at 2:45
  • @jgraup Great catch, I've updated the answer. – Christine Cooper Jul 29 '16 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 ;) – jgraup Jul 29 '16 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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