1

Bonsoir, I want to pass shortcode attributes to my class in a form of an array and the name of my shorcode but in the wordpress codex doesn't says how could i do that, this is my code

 $atributos=array('url' => ' ','text' => ' ');
 $shortcode_name="myshortcode";

 class MyLittleClass {
    public static function exe_short( $atts, $atributos, $shortcode_name) {

     $_atts = shortcode_atts($atributos, $atts );   

     // Attributes turn to variables
        $url = $_atts['url'];
        $text = $_atts['text']; 

        ob_start(); 
        include(ABS_DIR . '/includes/dynamics/'.$shortcode_name.'/index.php');  
        $content = ob_get_clean();
        return $content;
    }
 }

add_shortcode( $shortcode_name, array( 'MyPlugin', 'exe_short');

I know Im not passing the variables $atributos and $shortcode_name as an argumens for exeshort() in the function add_shortcode() but if i do this

add_shortcode( $shortcode_name, array( 'MyPlugin',exe_short($atts, $atributos, $shortcode_name))

It doesnt work either, how could I pass those variables to my class function ?

  • I believe you need a function outside of the class to add shortcode. Try removing the class and call a normal function. or encapsulating this whole thing into a function and call the shortcode after. – rudtek Nov 3 '17 at 19:33
  • but i need to define the same function and unfortunately is in the same script and will have issues, that's why i need a class to call his function and send that function my parameters – Johanna Ferreira Nov 4 '17 at 15:05
0

To create multiple shortcodes, you can get the default values from a static attribut of the class. This code create 2 shortcodes myshortcode1 and myshortcode2. In the included files, you can read variables $_atts, $content and $tag

class MyLittleClass {

    public static $shortcodes;


    public static function init() {

        self::$shortcodes = [
            "myshortcode1" => [
                "defaultValues" => [
                    "text" => "my link",
                    "url" => home_url("/"),
                ],
            ],
            "myshortcode2" => [
                "defaultValues" => [
                    "color" => "#EEC",
                ],
            ],
        ];


        foreach (array_keys(self::$shortcodes) as $code) {
            add_shortcode($code, [__CLASS__, "exe_short"]);
        }

    }

    public static function exe_short($attr, $content, $tag) {

        $atributos = self::$shortcodes[$tag]["defaultValues"];
        $_atts = shortcode_atts($atributos, $attr, $tag);

        ob_start(); 
        include(ABS_DIR . '/includes/dynamics/'.$tag.'/index.php');  
        $content = ob_get_clean();

        return $content;

    }

}

MyLittleClass::init();

you can also create shortcodes in passing an array at init or creating a filter when initializing $shortcodes.

  • I now this space is not made to say thanks but that code is what im looking for, thank you @mmm – Johanna Ferreira Nov 5 '17 at 2:03
0

If you cannot modify the method exe_short, you can create a new method like that

class MyLittleClass {

    public static function exe_short2($attr, $content, $tag) {

        $atts = $attr;
        $atributos = ["url" => "", "text" => $content];
        $shortcode_name = $tag;

        return self::exe_short( $atts, $atributos, $shortcode_name);

    }


    public static function exe_short( $atts, $atributos, $shortcode_name) {

        $_atts = shortcode_atts($atributos, $atts );

        // Attributes turn to variables
        $url = $_atts['url'];
        $text = $_atts['text']; 

        ob_start(); 
        include(ABS_DIR . '/includes/dynamics/'.$shortcode_name.'/index.php');  
        $content = ob_get_clean();
        return $content;

    }


}

add_shortcode( "myshortcode", array( 'MyPlugin', 'exe_short2');
  • I wan to do it outside the function in an very good format - Shortcode Name : xxx - Attributes : Array (xxx,xxx,xxx) and then the whole code do the thing, Imagine someone who doesnt know how to code, enters this code and in a manual says, change the shortocode name and the attributes in the array, then add xxx_file.php and with those attributes you define on the shortcode play with them – Johanna Ferreira Nov 4 '17 at 23:21
  • I add a new answer : wordpress.stackexchange.com/questions/284905/… – mmm Nov 4 '17 at 23:40

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.