skip to Main Content

I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.

I got it working. Somehow but not the way I intended to use it. I’m a total beginner. Please consider this when answering my questions.

I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.

When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don’t want to create a shortcode for each value.

I came up with this as a working solution…

// Honda

function honda() {

$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');

I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn’t able to get the right value displayed.

Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.

2

Answers


  1. Chosen as BEST ANSWER

    I came up with this / Which works in a way but... This isn't very comfortable to use.

    add_shortcode('HONDA','HONDA_TEST');
    function HONDA_TEST($atts = array(), $content = null, $tag){
        shortcode_atts(array(
            'var1' => 'default var1',
            'var2' => false,
            'var3' => false,
            'var4' => false
        ), $atts);
    
        if ($atts['var2'])
              return 'honda2';
        
        else if ($atts['var3'])
              return 'honda3';
        
        else if ($atts['var4'])
              return 'honda4';
        
        else
              return 'honda1'; 
    }
    

    So now when using:

    [HONDA var1="novalue"][/HONDA]
    [HONDA var2="novalue"][/HONDA] 
    [HONDA var3="novalue"][/HONDA] 
    [HONDA var4="novalue"][/HONDA]
    

    it shows:

    honda1
    honda2
    honda3
    honda4
    

    and so on.

    Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?


  2. I don’t have a working WP setup right now to test, but could you try this:

    add_shortcode('HONDA','HONDA_TEST');
    function HONDA_TEST($atts = array(), $content = null, $tag){
        $atts = shortcode_atts(array(
            'model' => 1,
        ), $atts);
    
        $myHondas = [1 => 'honda1', 'honda2', 'honda3'];
        
        return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
    }
    

    And use it with [HONDA model="1"], [HONDA model="2"]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search