0

I am trying to get shortcode attribute value to another function to add inline style.I want to use $color's value to another function.

Here is my code

<?php

/**
* 
*/
class MakeShortcode
{

    public $color;
    public function __construct()
    {
        add_action( 'init', array($this, 'PostBLock') );
        add_shortcode( 'PostBLock', array($this, 'getpostblock') );

        add_action( 'wp_enqueue_scripts', array( $this, 'inlinestyle') );

    }

    public function getpostblock( $atts ){
        extract( shortcode_atts( 
        array(
            'section_color' => '#000000',

        ), $atts) );

        $this->color = $section_color;

        $output = 'First LIne';
        $output .= 'Second Line';
        return $output;
    }
    public function inlinestyle(){
        wp_enqueue_style(
            'custom-style',
            get_template_directory_uri() . '/css/style.css'
        );
        $custom_css = "
            div[data-id=289374].post-block-post-left.active,
            div[data-id=289374].post-block-post-right.active{
                background : {$this->color};
                border-color : {$this->color};
            }";
        wp_add_inline_style( 'custom-style', $custom_css );
    }
}
$test = new MakeShortcode();

But background and border-color becomes empty.That means i am not getting $this->color value in that function.How i can get that?

1
  • wp_enqueue_scripts is called before the shortocode render. then if you want to choose a custom color in a page, add a meta box to select the color in the edit page.
    – mmm
    Oct 9, 2017 at 7:15

1 Answer 1

0
<?php
class MakeShortcode
{

public $color;
public function __construct()
{
    add_action( 'init', array($this, 'PostBLock') );
    add_shortcode( 'PostBLock', array($this, 'getpostblock') );

    add_action( 'wp_enqueue_scripts', array( $this, 'inlinestyle') );

}

public function getpostblock( $atts ){
    extract( shortcode_atts( 
    array(
        'section_color' => '#000000',

    ), $atts) );

    return $section_color;
}
public function inlinestyle(){
 $color = $this->getpostblock();  
 wp_enqueue_style(
        'custom-style',
        get_template_directory_uri() . '/css/style.css'
    );
    $custom_css = "
        div[data-id=289374].post-block-post-left.active,
        div[data-id=289374].post-block-post-right.active{
            background : {$color};
            border-color : {$color};
        }";
    wp_add_inline_style( 'custom-style', $custom_css );
}
}
$test = new MakeShortcode();
17
  • But i have something to return on getpostblock() .check the question now it would be something like That
    – Hudai
    Oct 9, 2017 at 10:29
  • in your inlinestyle() function you can use $this->getpostblock()->$color Oct 9, 2017 at 10:52
  • But not works.Here are some error: Warning: Missing argument 1 for MakeShortcode::getpostblock() Notice: Undefined variable: atts Notice: Undefined variable: color Notice: Trying to get property of non-object
    – Hudai
    Oct 9, 2017 at 11:08
  • what is your initial errror?...is WP_DEBUG set to true? Oct 9, 2017 at 11:14
  • Yes it was turned on .now i turned false..but not working .. $color is not echoing yet.. here is complete file what i am working with : pastebin.com/5ctWCgbu it is actually a visual composer shortcode
    – Hudai
    Oct 9, 2017 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.