0

I am using the shortcake UI plugin and working towards registering a series of shortcodes into the UI interface.

I am trying to grab all the taxonomy terms (via get_terms()) and then populating arrays for the interface. Right now I am getting a parse error as such:

Parse error: syntax error, unexpected ';', expecting ')' in...

here is the code:

// Register the Featured Pages shortcode with 'Shortcake UI' plugin
add_action( 'register_shortcode_ui', 'shortcode_ui_feat_pages' );

function shortcode_ui_feat_pages()
{
    shortcode_ui_register_for_shortcode(
        'featured-pages',
        array(
            'label' => 'Featured Pages Shortcode',
            'listItemImage' => 'dashicons-editor-table',
            'attrs' => array(
                array(
                    'label' => 'Category',
                    'attr' => 'category',
                    'type' => 'select',
                    'options' => array(
                    $feat_pages_terms = get_terms(array('taxonomy' => 'featured_page_category', 'hide_empty' => false));
                    foreach($feat_pages_terms as $feat_page_term) :
                        echo 'array(\'value\' =>\'' . $feat_page_term->name . '\', \'label\' =>\'' . $feat_page_term->name . '\'), ';
                    endforeach;
                    ),
                    'description' => 'Insert the category full name (i.e. Home page)',
                ),
                array(
                    'label' => 'Columns',
                    'attr' => 'cols',
                    'type' => 'select',
                    'options' => array(
                        array('value' => '', 'label' => esc_html__('')),
                        array('value' => 2, 'label' => esc_html__('Two')),
                        array('value' => 3, 'label' => esc_html__('Three')),
                        array('value' => 4, 'label' => esc_html__('Four')),
                    ),
                    'description' => 'The number of columns you want to display within a row',
                ),
                array(
                    'label' => 'Order By',
                    'attr' => 'orderby',
                    'type' => 'select',
                    'options' => array(
                        array('value' => '', 'label' => esc_html__('None')),
                        array('value' => 'ID', 'label' => esc_html__('ID')),
                        array('value' => 'author', 'label' => esc_html__('Author')),
                        array('value' => 'title', 'label' => esc_html__('Title')),
                        array('value' => 'name', 'label' => esc_html__('Name')),
                        array('value' => 'date', 'label' => esc_html__('Date')),
                        array('value' => 'modified', 'label' => esc_html__('Modified')),
                        array('value' => 'rand', 'label' => esc_html__('Random')),
                        array('value' => 'menu_order', 'label' => esc_html__('Menu order')),
                    ),
                    'description' => 'Choose how you want to order the Featured Pages',
                ),
                array(
                    'label' => 'Order',
                    'attr' => 'order',
                    'type' => 'select',
                    'options' => array(
                        array('value' => '', 'label' => esc_html__('')),
                        array('value' => 'ASC', 'label' => esc_html__('Ascending')),
                        array('value' => 'DESC', 'label' => esc_html__('Descending')),
                    ),
                    'description' => 'You can order the pages in ascending or descending order (A-Z or Z-A)',
                )
            ),
        )
    );
}

I have tested the foreach loop outside the code which registers the featured-pages array and it outputs fine. I'm just a bit stuck getting it to populate the options for select from the custom post type taxonomy 'featured_page_category'.

It just doesn't like me declaration to get_terms() when making the variable $feat_pages_terms.

Do I have to run this logic outside the shortcode ui registration and explode the values into it later?

3
  • you can't have lines of code inside an array!
    – inarilo
    Jun 6 '17 at 5:52
  • Oh ok, Could you possible suggest how I can get the values I need looped and printed into an array? Jun 6 '17 at 5:56
  • You are confusing this with php creating html, you are not creating the code using strings here :)
    – inarilo
    Jun 6 '17 at 6:13
1

This creates the multidimensional array in advance and then assigns it to options. The multidimensional array is created by assigning an array to each element of the main array. I have reused the $feat_pages_terms variable, but you can also create a separate variable using array() and assign array elements to that, I am including those lines as commented lines. When using this option, $key is not needed as new elements are simply being appended.

function shortcode_ui_feat_pages()
{
    $feat_pages_terms = get_terms(array('taxonomy' => 'featured_page_category', 'hide_empty' => false));
    //$multiarr = array();
    foreach($feat_pages_terms as $key => $feat_page_term) :
        $feat_pages_terms[$key] = array('value' => $feat_page_term->name, 'label' => $feat_page_term->name);
        //$multiarr[] = array('value' => $feat_page_term->name, 'label' => $feat_page_term->name);
    endforeach;
    shortcode_ui_register_for_shortcode(
        'featured-pages',
        array(
            'label' => 'Featured Pages Shortcode',
            'listItemImage' => 'dashicons-editor-table',
            'attrs' => array(
                array(
                    'label' => 'Category',
                    'attr' => 'category',
                    'type' => 'select',
                    'options' =>$feat_pages_terms/*$multiarr*/,
                    'description' => 'Insert the category full name (i.e. Home page)',
                ),
...
3
  • You sir, are my hero. Thank you so much. It appears so obvious to me now Jun 6 '17 at 6:15
  • you're welcome :) not a sir, though ;)
    – inarilo
    Jun 6 '17 at 6:22
  • he he, gotcha ;) Jun 6 '17 at 6:30

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.