0

I need to set a different shortcode for every day of the week, so I decided to do it using Visual Composer's Raw JS. So I want to use the javascript function getDay() to do that and print the shortcode I want for the specific day. Can this be done and how?

0

I know that you've asked for your answer in js but I couldn't comment to ask if you'd accept a solution written with PHP.

If you are happy to use PHP you can create a shortcode that accepts each of your shortcodes as arguments. You could even map this into visual composer with their shortcode mapping tool.

The example shortcode would look like this:

[change_shortcode_each_day mon="short_one" tue="short_two" wed="short_three" thu="short_four" fri="short_five" sat="short_six" sun="short_seven" default="default_short"]

note: I provided a default option just in case you don't have a shortcode for each day.

add_shortcode('change_shortcode_each_day', 'wpse_273181_change_shortcode_each_day');

function wpse_273181_change_shortcode_each_day($atts) {

    /**
     * You can set defaults here so you don't have to right the
     * args in the shortcode itself.
     * 
     * You cannot technically use a shortcode in a shortcode
     * so you need to type the shortcodes without the brackets
     */
    $unprepared_shortcodes = shortcode_atts(
        array(
            'mon'     => '',
            'tue'     => '',
            'wed'     => '',
            'thu'     => '',
            'fri'     => '',
            'sat'     => '',
            'sun'     => '',
            'default' => '',
        ),
        $atts
    );

    //make each shortcode 'complete' e.g [short_one]
    foreach ($unprepared_shortcodes as $key => $value) {
        $prepared_shortcodes[$key] = '[' . $value . ']';
    }

    extract($prepared_shortcodes);

    $current_day = date('N');

    switch ($current_day) {
        case '1' :
            $short_to_use = $mon;
            break;
        case '2' :
            $short_to_use = $tue;
            break;
        case '3' :
            $short_to_use = $wed;
            break;
        case '4' :
            $short_to_use = $thu;
            break;
        case '5' :
            $short_to_use = $fri;
            break;
        case '6' :
            $short_to_use = $sat;
            break;
        case '7' :
            $short_to_use = $sun;
            break;
    }

    /**
     * check to make sure the we have a shortcode for the day
     * if we don't use the default.
     * 
     * return false if both short_to_use and default are 'blank'
     */

    if($short_to_use == '[]' && $default != '[]') {
        $short_to_use = $default;
    } else if ($short_to_use == '[]' && $default == '[]') {
        return false;
    }

    //if we're still here do the shortcode
    return do_shortcode($short_to_use);
}
  • very nice, but where can i add my php code to make this work? – panbak Jul 13 '17 at 9:58
  • Thanks, you can add this to your themes (or child-themes) functions.php and then use it like any other shortcode. – mrmadhat Jul 13 '17 at 18:34

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.