1

I've never done this before so am looking to learn how to do this. I am working in wordpress and creating a plugin. I have a function at the top of my document that adds some jQuery code to the footer of my page, the simplified version of this is as follows:

function slug_masonry_init() { ?>
<script>
jQuery(function($) {
var container = document.querySelector("#skizzar_masonry_loop");
var msnry = new Masonry( container, {
    columnWidth: <?php echo $masonry_item_width ?>,
    gutter: <?php echo $masonry_item_padding ?>,
    itemSelector: ".skizzar_masonry_entry"
});

});
</script>
<?php }
//add to wp_footer
add_action( 'wp_footer', 'slug_masonry_init' );

You will notice within the <script> there are some php variables: $masonry_item_width and $masonry_item_padding

These variables are user defined, the function that handles these is a little further down my document and looks like this:

function skizzar_masonry_shortcode($atts,$content = null) {
    global $skizzar_masonry_item_width, $skizzar_masonry_item_padding;
    $el_class = '';
        extract(shortcode_atts(array(
            "masonry_item_width" => '240',
            "masonry_item_padding" => '5',
            "el_class" => '',
        ),$atts));

        $skizzar_masonry_item_width = $masonry_item_width;
        $skizzar_masonry_item_padding = $masonry_item_padding;

        $output .= '<style>.skizzar_masonry_entry, .skizzar_masonry_entry img {width:'.$masonry_item_width.'px;} .skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:560px; /* + padding */} .skizzar_masonry_entry {margin-bottom:'.$masonry_item_padding.'px; overflow:hidden;}</style>';
        $output .= '<div id="skizzar_masonry_loop" class="'.$el_class.'">';
        $output .= do_shortcode($content);
        $output .= '</div>';

        return $output;
    }

SIDE NOTE: the two global variables you see here: $skizzar_masonry_item_width and $skizzar_masonry_item_width are defined a little further up the document (between the two code snippets) - this is the user interface for inputting width and padding, the code you see here creates a shortcode.

What I don't know how to do is add $masonry_item_padding and masonry_item_width to my jQuery script at the top of the page - currently they are just returning blank.

1

2 Answers 2

3

You have to echo variables for them to output

columnWidth: <?php echo $masonry_item_width ?>,
gutter: <?php echo $masonry_item_padding ?>,

As to how to get your values... Wordpress makes plugins such a mess with all these standalone callbacks. Normally I would abhor doing this but I can't think of any other way to make this work. I'm putting them into an array to make for fewer globals. This part goes in your skizzar_masonry_shortcode

$GLOBAL['skizzarvar'] = array(
   'width' => $masonry_item_width, 
   'padding' => $masonry_item_padding
);

Then you can do this in your slug_masonry_init

<?php echo $GLOBAL['skizzarvar']['width'] ?>
Sign up to request clarification or add additional context in comments.

8 Comments

ah yes, well noticed. I have added it in though and still the same issue of the variables not being outputted...I assume because they are outside of the function
I hate how Wordpress does their callbacks because it makes for situations like these. Added something that should help you.
Annoyingly that still didn't work, although I understand what you are saying! Could it be anything to do with the position of my functions within the document?
It could be, although I would except short tags to be processed first. Try doing a simple echo from each function and see which fires first.
Cool, my apologies but I'm still feeling my way through php - how do I use echo to see which function is firing first?
|
1

all you have to do is:

function slug_masonry_init() 
{ 
    global $masonry_item_width, $masonry_item_padding;
    ...

See more about global keyword here: http://php.net/manual/en/language.variables.scope.php

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.