I've been trying to create a shortcode for the Category Ajax Chain Selects plugin so that I can display the chain selects within a chosen post/page easily, but for now I've only been able to hardcode the attributes into the shortcode:

function cross_ref_lookup_shortcode() {
    echo chainselect_getcategories( 'kla-competitor', 2, array('Select Manufacturer', 'Select Product'), array('Manufacturer', 'Product'), 'Find comparable product', '', 0 );

add_shortcode( 'cross_ref_lookup', 'cross_ref_lookup_shortcode' );

This works fine and I can display the chain selects where I want, but I want to create a shortcode where I can add the variables as shortcode attributes.

The usage of the chainselect_getcategories function looks like this:

<?php chainselect_getcategories( $tax, $level, $titles, $labels, $btn_text, $exclude, $count ) ?>

I got far enough that everything worked except for the labels and titles, because they require an array. I can't figure out how to pass a shortcode attribute as an array to the function so that this will work as intended. In my hardcoded example above, the arrays work, and this is what I need to recreate from the shortcode attribute. Here's as far as I got:

function chain_select_shortcode( $atts ) {
    extract( shortcode_atts( array( 'tax' => 'category', 'level' => 1, 'titles' => array( 'Please select…' ), 'labels' => '', 'btn_text' => 'Submit', 'exclude' => '', 'count' => 1 ), $atts ) );

    return chainselect_getcategories( $tax, $level, explode( ',', $titles ), explode( ',', $labels ), $btn_text, $exclude, $count );

add_shortcode( 'ajax_chain_select', 'chain_select_shortcode' );

Any ideas on how to complete this?

  • That looks like it should work, though breaking on something as common as a comma may be prone to error. Where does it fail?
    – s_ha_dum
    Jul 29, 2013 at 14:40
  • Well, for example, if I use the shortcode [ajax_chain_select tax="kla-competitor" level="2", titles="Select Manufacturer,Select Product" labels="Manufacturer,Product" count="0"], the result is "Error :: Number of labels don't match number of levels", which doesn't occur when it's hard-coded into the shortcode as in the first example. Jul 29, 2013 at 17:52
  • Interesting. I can't test anymore without the code for chainselect_get_categories() but I did check that the arrays are built correctly and they seem to be.
    – s_ha_dum
    Jul 29, 2013 at 18:03

1 Answer 1


$level must be passed as an integer, not as a string.

The problem is with the plugin code:

if (count($labels) !== $level && $labels !== '') { 
    echo 'Category Chain Select Plugin</br>Error :: Number of labels don\'t match number of levels';

The !== operator requires a match on type as well as value. Had the author used != a string value for $level would match. The test for $titles uses == which does not check type.

    if (count($titles) == $level) {

You might point out this inconsistency to the plugin author.

  • That did it. I made the change to the plugin manually and it worked. I have contacted the plugin author and pointed out this discrepancy and also offered up my code for him to include in the plugin for other users. Hopefully he includes it. Thanks Charles Jul 30, 2013 at 12:19

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.