1

I know I'm bad with coding, but I tried whatever I could find on the internet, and I ended up with this, which is not giving an error, but also when using the shortcode [my_playlist] in a post, only a part is showing "Listen to the full score songs playlist frompostname"

Can you please tell me what am I doing wrong, how should I make the rest of the html after the_title(); display? thank you so much


// function that runs when shortcode is called
function wpb_playlist_shortcode() { 
 
// Things that you want to do. 
$string .= '<center>'  . _e(' Listen to the full score songs playlist from', 'wpml_theme') . '<strong>' . the_title();  ' </strong> 
<br/>
<span class="playlist"><img class="middle" src="https://example.com/images/play-this-song.png" /> YouTube</span> <br/> <span class="playlist2"><img class="middle" src="https://exampl.com/images/play-this-song.png" /> Deezer</span> <br/><span class="playlist3"><img class="middle" src="https://exampl.com/images/play-this-song.png" /> Spotify</span></center>'; 
 
// Output needs to be return
return $string;
}
// Register shortcode
add_shortcode('my_playlist', 'wpb_playlist_shortcode'); 
 
1
  • @Cyclonecode Shortcodes MUST return strings, they MUST NOT create output, eg. use print or echo.
    – fuxia
    Jan 30, 2022 at 22:09

2 Answers 2

2

There are several things you need to know, after which the problems become obvious:

  • you join strings together using . for example 'hello' . 'world' is the same as 'helloworld'
  • You end a statement/action/call with ;, this is you telling PHP that you have finished saying something, and you are going to say something new next, like a fullstop in a sentence
  • shortcodes must return their content as a string, you can't use functions that echo or output directly to the browser, and you can't use . to attach them to a string. the_title is one of those functions
  • you don't have to put it all on a single line, it makes it very difficult to read. What are you going to do when it says the problem is on line 5 but line 5 has tonnes of stuff on it? ¯\_(ツ)_/¯ it's like asking for directions and just being told "Somewhere north"
  • $string .= 'stuff'; is short hand for $string = $string . 'stuff';
  • you can't pull stuff out of thin air, e.g. $string, you ask PHP to append stuff to it but where did it come from? What is $string? You need to give things initial values. PHP is going to go "They just asked us to add stuff to it but I've never heard of it, I'll just create a new variable and pretend it's "" and put a note in the PHP error log, hopefully this doesn't break anything 🤞". Think of it like being asked to go give Mary her coffee, who's Mary? You've never been introduced to Mary, PHP is basically the same as you just making up an imaginary friend called Mary on the spot so it still makes sense.

Thankfully there isn't anything in your HTML snippet that actually requires a shortcode other than automatically getting the title.

The above bullet points should make it clear what went wrong and how to fix your shortcode, but for the future, here's a much easier method: output buffers.

function myshortcode( $atts ) : string {
    ob_start();
    ?>
    <p>Your HTML goes here, and it'll get intercepted and returned as a string variable when `ob_get_clean` is called.</p>
    <?php
    return ob_get_clean();
}

Now you can work the same way as ordinary PHP template files in your shortcode, and it even has type hinting so if you return something other than a string it'll complain and tell you exactly what went wrong.

1
  • your answer is not just solving the problem, but it's pure genius! I actually understood. loved the way of explaining, and also the humor. hats off. thank you so much for the time
    – vyperlook
    Jan 31, 2022 at 0:36
1

You code contains a couple of errors which should be solved below:

function wpb_playlist_shortcode() { 
  // Assign to the variable and change from the_title() ; to get_the_title() .
  // since we need to concatenate the string
  // Also, we need to use __() instead of _e() since the later will echo instead of returning the output
  $string = '<center>'  . _e(' Listen to the full score songs playlist from', 'wpml_theme') . '<strong>' . get_the_title() .  ' </strong> 
             <br/><span class="playlist"><img class="middle" src="https://example.com/images/play-this-song.png" /> YouTube</span> <br/> <span class="playlist2"><img class="middle" src="https://exampl.com/images/play-this-song.png" /> Deezer</span> <br/><span class="playlist3"><img class="middle" src="https://exampl.com/images/play-this-song.png" /> Spotify</span></center>'; 

   // return the output
   return $string;
}
3
  • can you explain what those errors are and how you fixed them without it being a copy paste solution? I see you wrote comments but those who would find them most useful are those least likely to notice they're comments.
    – Tom J Nowell
    Jan 30, 2022 at 22:17
  • Also it still uses the_title which is incorrect
    – Tom J Nowell
    Jan 30, 2022 at 22:17
  • you should undelete this, it's still useful and you can still get upvotes
    – Tom J Nowell
    Jan 31, 2022 at 2:43

Your Answer

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

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