Object Orientation: Access attribute from outside shortcode wordpress

Question

I’m new to object orientation and I’m trying to create a class so that I can get the attributes set in the shortcode from a template file. But with the code below I still can’t access the attribute.

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 1 month 0 Answers 9 views 0

Leave an answer