Home>Support>How can i get custom taxonomy select dropdown as field in widget

How can i get custom taxonomy select dropdown as field in widget

Hi

Below is code i am using in widget but i can’t get option of select field. I see document guide select field type require associative array so get_search_course_cat function also return associative array term id as keys, term names as values but i am fail to get select field with taxonomy option. Please guide me


array(
            'title' => array(
                'type' => 'text',
                'label' => __('Title', 'edupress'),
                'default' => __('Search By Category', 'text-domain'),
            ),
			'course_cat' => array(
				'type' => 'select',
				'label' => __( 'Choose a thing from a long list of things', 'edupress' ),
				'multiple' => true,
				'options' => $this->get_search_course_cat(),
    		),
			
        ),
function get_search_course_cat()
{
	return get_terms( 'course_category', 'orderby=name&fields=id=>name&hide_empty=0' );
		
}

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 8 years, 7 months ago Alex S
    Hi, I Work Here

    Hi ahmed17,

    I don’t think you’ll be able to use get_terms() directly as it returns invalid data. As per the docs, you’ll want your array to be structured like so:

    array(  'field_key'   => __( 'Option name',  'widget-form-fields-text-domain' ),  'field_key_2' => __( 'Other Option', 'widget-form-fields-text-domain' ),)

    Ignoring that issue, get_terms() by default will return an object. Please refer to the codex for more information on how to return an associative array

  2. 8 years, 7 months ago ahmed17

    I am using get_terms function https://developer.wordpress.org/reference/functions/get_terms/ .

    You can see $args argument list in above codex url there is fields argument ‘id=>name’ (returns an associative array with ids as keys, term names as values), i double check it’s return array like

    array(
    '1' => 'Development',
    '2' => 'Mobile Apps',
    )
    

    Thanks
    Ahmed

  3. 8 years, 7 months ago Alex S
    Hi, I Work Here

    Hi Ahmed17,

    Well, I feel silly – You’re right. I’ll ask the development team about this as local tests say there’s nothing wrong with your query.

  4. 8 years, 7 months ago Greg Priday
    Hi, I Work Here

    One possibility is that because of the initialization order of WordPress, the get_terms function isn’t returning anything when the widget is being initialized.

    One option would be to use the modify_form function to add in the options later. This function is only called right before the form is rendered, so there’s a much better chance that get_terms is able to return something.

    Here’s an example of using modify_form:

    https://github.com/siteorigin/so-widgets-bundle/blob/develop/widgets/features/features.php#L269

    • 8 years, 7 months ago ahmed17

      Greg,

      Thank you so much it’s working.

  5. 8 years, 7 months ago ahmed17

    Greg,

    How Can i access widget field from outside widget ? Actually i am using media field to input image for widget background so i want to access image field outside widget. I know there is way we can apply style using style property of div but i am developing themeforest theme so it’s not allow to generate css using style property for validation purpose.

    For example i am using below bgimage field in widget how can i access it from outside widget?

    
    
    class Hello_World_Widget extends SiteOrigin_Widget {
    	function __construct() {
        //Here you can do any preparation required before calling the parent constructor, such as including additional files or initializing variables.
    
        //Call the parent constructor with the required arguments.
        parent::__construct(
            // The unique id for your widget.
            'hello-world-widget',
    
            // The name of the widget for display purposes.
            __('Hello World Widget', 'hello-world-widget-text-domain'),
    
            // The $widget_options array, which is passed through to WP_Widget.
            // It has a couple of extras like the optional help URL, which should link to your sites help or support page.
            array(
                'description' => __('A hello world widget.', 'hello-world-widget-text-domain'),
    			'panels_groups' => array('edupresshome1'),
                'help'        => 'http://example.com/hello-world-widget-docs',
            ),
    
            //The $control_options array, which is passed through to WP_Widget
            array(
            ),
    
            //The $form_options array, which describes the form fields used to configure SiteOrigin widgets. We'll explain these in more detail later.
            array(
                
                'bgimage' => array(
    				'type' => 'media',
    				'library' => 'image',
    				'label' => __('Background Image 1970x1323 px', 'edupress'),
    				'fallback' => true,
    			),
            ),
    
            //The $base_folder path string.
            plugin_dir_path(__FILE__)
        );
    	}
    
    

    Thanks

  6. 8 years, 7 months ago Greg Priday
    Hi, I Work Here

    Hi Ahmed

    To answer your question, it is technically possible to access the widget data, outside the widget, but it’s very complicated. You’d essentially need to look at the code WordPress or Page Builder (if you’re using it) use to fetch the widget data from the database and recreate that. WordPress stores widget instances in a global variable and Page Builder stores it in post meta.

    So while it’s technically possible, it’s a little complex, so finding a way to implement what you need using only the widget function would be ideal.

    • 8 years, 7 months ago ahmed17

      Greg,

      Thank you so much. I really appreciate your help.

      Thanks
      Ahmed

  7. 8 years, 6 days ago roya

    It is sooo important to me – but I didn’t understand it.

    Is there an possibility to add a custom dropdown to select a category? The build in post-loop-option misses some options i need urgently like the post-offset. But to be honest I have absolutly NO glue how to move on. An example to show how to add such a dropdown would be really really cool!

    Thank you

    • 8 years, 6 days ago Alex S
      Hi, I Work Here

      Hi Roya

      Unfortunately, we can’t help you with doing this as it’s beyond the scope of what we can help you with on the forums. With that said, you could hire a developer to implement this for you if it’s important to your website. We highly recommend using Codeable.

      To clarify, you’re able to set an offset in the Post Loop widget via the additional field.

  8. 8 years, 5 days ago roya

    Hi Alex,

    I like you guys, you’re responding so fast for a free plugin. Thank you so far!

    I am pretty shure I could do it my own, I am just a little bit lost.
    For the moment I’ll use the additional field (for me for testing purposes it’s enough, but I think it’s not comfortable for the enduser as they have no idea about hooks and loops).

    All the best

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More