Access attribute from outside shortcode wordpress

Question

Eu sou novo em orientação a objetos e estou tentando criar uma classe para que eu possa obter de em um arquivo de template os atributos setados no shortcode. Mas com o código abaixo ainda não consigo acessar o atributo.

The output of the shortcode with the attributes is like this: [youlike id_post=’6059,73862′ link_utm=’?utm_campaign=test’]

Class Code

if ( !class_exists( 'Shortcode' ) ) {
    class Shortcode {
        private $atts;
        
        public function setAtts( $atts ) {
            $this->atts = $atts;
        }

        public function getAtts() {
            return $this->atts;
        }

        public function __construct() {
            add_shortcode('youlike', array($this, 'youCanLike'));
        }

        public function youCanLike( $atts ) {
            ob_start();

            $value = shortcode_atts( array(
                'id_post' => '',
                'link_utm' => '',
            ), $atts );

            $this->setAtts($atts);

            $post_ids_array = explode(',', $value['id_post']);
            $post_current = get_queried_object_id();

            $exclude = array( $post_current );
            $include = $post_ids_array;

            $args = array(
                'ignore_sticky_posts' => true, // Performance
                'no_found_rows' => true, // Performance
                'post__in'  => array_diff( $include, $exclude ),
                'orderby' => 'date',
                'order' => 'DESC'
            );

            get_template_part( 'global-templates/blocks/block', 'you-can-like', $args );

            return ob_get_clean();  
        }
    }
}
$obj = new Shortcode();

Link output in the template file

<a href = "<?php the_permalink(); ?><?php $linkUTM = new Shortcode(); $linkUTM->setAtts('link_utm');echo $linkUTM->getAtts(); ?>"

0
Renan Bessa 2 months 0 Answers 9 views 0

Leave an answer