Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi there!

    I don’t mind adding this modification to the plugin, but I’m curious though: why do you need this?

    Thread Starter agileinformatique

    (@agileinformatique)

    A lot of people want to use shortcode for get post with the current category which your great plugin can’t do now.

    there is a guy that propose a solution for that : https://blog.eexit.net/php-wordpress-popular-posts-for-the-current-category/

    But use a lot of resources for nothing.

    I think the best pratrice is to add an option to the shortcode:
    [wpp cat=”current”]

    and just add this code in the function.php

    function sc_wpp_set_current_category( $out, $pairs, $atts ) {
    
    if ( is_category() ) {
       $catID = get_query_var('cat');
       if (array_key_exists("cat",$atts)) {
          if ($atts['cat'] == "current"){
             $atts['cat'] = $catID;
            }
         }
      }
    $out['cat'] = $atts['cat'];
    return $out;
    }
    
    add_filter( 'shortcode_atts_wpp', 'sc_wpp_set_current_category', 1, 3 );

    but in the core of wordpress (shortcodes.php)
    the filter can’t call, if the function shortcode_atts don’t give the shortcode name :

    /**
     * Combine user attributes with known attributes and fill in defaults when needed.
     *
     * The pairs should be considered to be all of the attributes which are
     * supported by the caller and given as a list. The returned attributes will
     * only contain the attributes in the $pairs list.
     *
     * If the $atts list has unsupported attributes, then they will be ignored and
     * removed from the final returned list.
     *
     * @since 2.5.0
     *
     * @param array  $pairs     Entire list of supported attributes and their defaults.
     * @param array  $atts      User defined attributes in shortcode tag.
     * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
     * @return array Combined and filtered attribute list.
     */
    function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
    	$atts = (array)$atts;
    	$out = array();
    	foreach ($pairs as $name => $default) {
    		if ( array_key_exists($name, $atts) )
    			$out[$name] = $atts[$name];
    		else
    			$out[$name] = $default;
    	}
    	/**
    	 * Filter a shortcode's default attributes.
    	 *
    	 * If the third parameter of the shortcode_atts() function is present then this filter is available.
    	 * The third parameter, $shortcode, is the name of the shortcode.
    	 *
    	 * @since 3.6.0
    	 * @since 4.4.0 Added the <code>$shortcode</code> parameter.
    	 *
    	 * @param array  $out       The output array of shortcode attributes.
    	 * @param array  $pairs     The supported attributes and their defaults.
    	 * @param array  $atts      The user defined shortcode attributes.
    	 * @param string $shortcode The shortcode name.
    	 */
    
    	if ( $shortcode ) {
    		$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
    	}
    
    	return $out;
    }
    Plugin Author Hector Cabrera

    (@hcabrera)

    Understood, thanks for the explanation ??

    I’ll add the “wpp” part into the shortcode_atts function on the next release so you can hook into it without having to hack the code.

    Thread Starter agileinformatique

    (@agileinformatique)

    thank you very much ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add shortcode name for enable shortcode_atts filter’ is closed to new replies.