I am trying to build a function, which gives out different image-urls, according to the last digit of bbp_get_topic_id().
This is, what i've got so far:

function bg_image_topic_titlearea() {
  $letztezifferimgs = substr("'.bbp_get_topic_id().'", -1);
  switch ($letztezifferimgs) {
    case "0":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/10/Blockaden-lösen-2.jpg";
    case "1":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/09/Das-Leben-ist-ein-Spiel-2.jpg";
    case "2":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/02/Bedürfnisse-erkennen-3.jpg";
    case "3":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/08/Selbsterkenntnis-sich-selbst-finden2-1.jpg";
    case "4":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/11/Reinkarnation-Wiedergeburt-2.jpg";
    case "5":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/10/Intuition3.jpg";
    case "6":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/07/Manchmal-muss-man-loslassen-3.jpg";
    case "7":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/07/Manchmal-muss-man-loslassen-3.jpg";
    case "8":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/02/Pfad.jpg";
    case "9":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/01/Zirbeldrüse-aktivieren-2.jpg";

Yet it does not react, if i use .bg_image_topic_titlearea(). to get the image url.
I am trying to get it done for some time now and i would be glad, if someone could help me.

Kind regards

  • Please give an example of the full statement you're using to get the image url, as there might be a problem there, too. – CK MacLeod Mar 29 '17 at 16:49
  • Its a bundle of shortcodes, where i want to include the outcome of this switch statement. It works for sure, because, if i enter an image url directly, it works also. – Dominik Baumgartner Mar 29 '17 at 20:29
  • Well even one typical statement would be useful. – CK MacLeod Mar 29 '17 at 20:33
  • I am not sure, if i get your question right. This is, where i want to include the outcome: function bbpress_topic_add_titlearea() { echo do_shortcode('[et_pb_section bb_built="1" admin_label="section" background_image=" '.bg_image_topic_titlearea().'...' ) } – Dominik Baumgartner Mar 29 '17 at 20:44
  • The fragmentary snippet you're provided is obviously incomplete. I'll just say not that incorporating a function as shortcode attribute strikes me as unusual. I think if you provided the ENTIRE shortcode function or set of functions, the problems would be easier to fix in detail. For now I'll provide a guess as to the problem below. – CK MacLeod Mar 29 '17 at 23:04

I would try something like this:

function bg_image_topic_titlearea() {

    if ( ! function_exists( 'bbp_get_topic_id' ) ) {
        return false;

    if ( ! $letztezifferimgs = bbp_get_topic_id() ) {
        return false;

    $letztezifferimgs = substr( $letztezifferimgs, - 1 );

    // switch ...

    return true;


I would recommend checking to make sure the function call (bbp_get_topic_id();) to retrieve the Topic ID exists.

I would recommend checking to see if there's actually a value received from the function call to retrieve the Topic ID.

Not sure why you need to use substr();, but perform that once you know you have received a Topic ID.

THEN perform your switch.

I would also recommend placing a "default case" in your switch statement.

Example of a "default case":


    // do something...

  • The substr() is to get the last digit of the topic_id, because it has more than one. Does this change anything on how the function should be developed? – Dominik Baumgartner Mar 29 '17 at 20:38

Without seeing the full code in question, it's difficult to isolate its problems. So, please forgive me if I've misunderstood.

I'll just add to what I was suggesting in my comments above that a more typical shortcode implementation would be 1) to have the image-url function return, not echo, its output, and 2) to use a simple string as shortcode attribute, not a function returning a string. Typically, the function or its arguments would already have returned the string within the shortcode function.

Further on return v echo, the Codex states:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

It looks, in short, like you're trying to return an echo, or maybe echo an echo. (I'll also add that, at least up until today when I made the same mistake a couple or three times, using an echo or equivalent is a mistake I make or have made frequently - sometimes you can get away with it, and sometimes it produces unexpected or simply bad results.)

As for the second part, incorporating a function as an attribute when placing the shortcode wherever it goes, I've never tried and I don't think I've ever run across it, though it's hard to be sure what you're doing or to test it, based on what's presented in the question. In any event .bg_image_topic_titlearea(). seems to suggest an intention to, again, return rather than echo a string within a longer statement - i.e., echo 'Here's the image url: ' . bg_image_topic_titlearea() . 'How do you like it?'; For that, you'd want a returned value, not an echoed statement.

  • Thanks for your answer, i am relatively new to php and just try to achieve, to get 10 different background images, which should be alternately used for the topic pages of my forum. Here you can see a page, where the image should be visible: test.bewusstewelt.de/topic/test-2/#post-4813 (behind the "Test"-text and the topic info.) If i just insert an image url in the shortcode inside the function, which i call for, on the page template, it works fine, so the shortcode is no problem. Its just that the .bg_image_topic_titlearea(). function does not return anything. – Dominik Baumgartner Mar 30 '17 at 11:57
  • Well, you might use something like Michael Ecklund's suggestion, except, in the switch/case part, implement a new variable - e.g.: $image_url,. In each "case" state $image_url = "http://...", instead of echoing anything, then return $image_url at the end of the function. That function would then be called by the shortcode or whatever other function. I cannot guarantee this would work, however, because I don't know exactly how you're implementing it – CK MacLeod Mar 30 '17 at 17:40

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.