1

Hi – in my theme (mystile) i am limited to determine the number of featured products displayed on the homepage. But I cannot assign the columns. Shortcode does not apply because the homepage is generic and cannot be altered in the backend. I can, of course change the number of columns in the plugin source file (class-wc-shortcode.php, line 76), but that will be lost with the next update. I am not very good with php and i scanned similar questions. I tried several methods to filter this value but none of them worked. Here is my approach:

/*different values for featured prod shortcode*/
function my_shortcode_val( $atts ) {
$atts = shortcode_atts( array(
        'per_page' => '12',
        'columns'  => '2',
        'orderby'  => 'date',
        'order'    => 'desc'
    ), $atts );
return $atts;
}
add_filter( 'featured_products', 'my_shortcode_val' );

But that does not work. Is there a way to override the value for columns or any shortcode value? Thanks for your help.

1 Answer 1

2

Alright I want to post this here because of the title of your question.

There is a way to change shortcode attributes, but it will also rely on what the developer of the plugin has done. When a plugin is developed with a shortcode there is an option to use the shortcode_atts() function. This happens most of the time I would say but what some people don't know is that there is a way to let this be modified with a filter. In most plugins you will see something like this:

shortcode_atts( array(
                       'name'                  => get_bloginfo( 'title' ),
                       'description'           => '',
    ), $atts );

This is fairly common, however there is one more attribute that can be used:

shortcode_atts( array(
                       'name'                  => get_bloginfo( 'title' ),
                       'description'           => '',
    ), $atts, 'shortcode_name' );

See the shortcode_name that got added as a parameter? Well what this does is creates a filter in WordPress that can be used by others. The format to add a filter for this now looks like this:

add_filter( 'shortcode_atts_shortcode_name', 'do_something', 10, 3 );

So we can create an actual filter function like this:

    function do_something( $out, $pairs, $atts ) {      
        if( empty( $out['name'] ) ) {
            $out['name'] = 'This will never be empty now';
        }

        return $out;
    }
   add_filter( 'shortcode_atts_shortcode_name', 'do_something', 10, 3 );

References:

https://codex.wordpress.org/Function_Reference/shortcode_atts http://hookr.io/4.1.1/filters/shortcode_atts_shortcode/

Ok so I wrote that up in case others get to this page based on your title.

I think that your problem is a bit different - and also WC does not seem to have it setup to allow the filter I explained above.

Looking at their code around where you pointed out it looks like there is an action hook you can hook into: <?php do_action( "woocommerce_shortcode_before_{$loop_name}_loop" ); ?>

If you can figure out what the $loop_name is for your specific case then I image something like this should work (untested):

function change_wc_col() {
    global $woocommerce_loop;

    $woocommerce_loop['columns'] = 3; // Or whatever number of columns you want here
}
add_action( 'woocommerce_shortcode_before_{$loop_name}_loop', 'change_wc_col' );

That seems like it would change the columns value that you stated you are looking to change.

Sorry for the wall of text, but I hope this helps set you in the right direction at least.

5
  • 1
    Please don't use extract() in shortcodes, or any other functions. It was completely removed from core for very specific reasons and the codex was also updated accordingly. Commented May 12, 2015 at 5:44
  • @PieterGoosen Yup 100% agreed. I don't use them in my code and I should have just kept them out. I was trying to keep it close to what many plugins will look like for example purposes. I have removed them though to keep the example code in compliance with WP standard coding practices. Thanks for you input.
    – Nick Young
    Commented May 12, 2015 at 5:47
  • @NickYoung – i tested your proposal. Sorry, it doesn't work. But i figured out a another solution. It is of course not very elegant, but i can bring the whole public function from class-wc-shortcodes.php into my functions.php. Then i can alter the values. But it's a crude solution. Thanks for your help.
    – timholz
    Commented May 12, 2015 at 16:32
  • @timholz no problem. I wasn't sure if it would be the right solution. I wanted to get the information about changing shortcode attributes out there and then hopefully get you started in the right direction. Glad you were able to get it working!
    – Nick Young
    Commented May 12, 2015 at 22:19
  • @NickYoung – thanks.Your help is much appreciated.
    – timholz
    Commented May 13, 2015 at 14:26

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.