Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

Dynamic javascript plugin settings

I am creating a wordpress plugin using code from:
http://tympanus.net/codrops/2012/12/11/fullscreen-pageflip-layout/

I have everything working but I am trying to tidy up and clean up the plugin code and noticed that I am creating the initialization / settings for every "BookBlock" I have, not just for the one that is on the page.

Each BookBlock could have different settings controling the look and feel.

bb = $( '#bb-bookblock' ).bookblock( {
        speed : 800,
        perspective : 2000,
        shadowSides : 0.8,
        shadowFlip  : 0.4,
      ...etc.
} ),

My current footer code is looping through all the BookBlocks and adding the settings.

foreach ($options['BookBlocks'] as $BookBlock) {
      speed : <?php echo $BookBlock) ['settings']['speed']; ?>,
}

I only want to show the settings for the BookBlock(s) that are loaded on that page.

The plugin is using a shortcode [BookBlock 'name']


I have and add_action wp_footer that calls the for each code above.

I appreciate any help, this is my first attempt to create a plugin / user interface to call some code.
0
wsadfilm
Asked:
wsadfilm
  • 6
  • 5
1 Solution
 
leakim971PluritechnicianCommented:
what about :


<?php

	foreach ($options['BookBlocks'] as $BookBlock) {
		echo "var php_speed = " . $BookBlock['settings']['speed'] . ";";
		echo "var php_perspective = " . $BookBlock['settings']['perspective'] . ";";
		echo "var php_shadowSides = " . $BookBlock['settings']['shadowSides'] . ";";
		echo "var php_shadowFlip = " . $BookBlock['settings']['php_shadowFlip'] . ";";
	}

?>


	bb = $( '#bb-bookblock' ).bookblock({
        speed : php_speed,
        perspective : php_perspective,
        shadowSides : php_shadowSide,
        shadowFlip  : php_shadowFlip
	});

Open in new window

0
 
wsadfilmAuthor Commented:
Thanks for the reply.

I might be missing something in your suggestion.

I am not having a problem getting settings for the BookBlock, everything works.  What I am trying to do is only have the code for the BookBlocks that is on a specific page.

EXAMPLE:
Say I have 5 BookBlocks created on the site (1 for each page).  As the code is written I get:
bb = $( '#bb-bookblock-block1' ).bookblock({settings here});
bb = $( '#bb-bookblock-block2' ).bookblock({settings here });
bb = $( '#bb-bookblock-block3' ).bookblock({settings here });
and so on, for every bookblock on the page that have a bookblock shortcode included.  I am trying to figure out how to only get the script code for the specific BookBlock that is on that page.

I think my issue is with the foreach – I don’t think I should have the for loop but, but I don’t know how to pass in the ‘slug’ for the specific BookBlock I want.  

I am struggling in how to pass in the slug for the bookblock that is used on that page for the plugin to create the script code in the footer.

I am wondering if I am storing the data incorrectly in wordpress.  The plugin data is all stored in a single record in the wp_options table.  I have one record in wp_options and using arrays in the php to get.

Should I consider separating this and using a custom post instead?  But I am still not sure how to limit the wp_footer action to only have the script code the [BookBlock ‘block1’]
0
 
leakim971PluritechnicianCommented:
Please provide a link to your page
var bb = null;
if( $( '#bb-bookblock-block1' ).length > 0 ) {
    bb = $( '#bb-bookblock-block1' ).bookblock({settings here});
}
else if( $( '#bb-bookblock-block2' ).length > 0 ) {
    bb = $( '#bb-bookblock-block2' ).bookblock({settings here});
}
else if( $( '#bb-bookblock-block3' ).length > 0 ) {
    bb = $( '#bb-bookblock-block3' ).bookblock({settings here});
}

Open in new window


or maybe you know on which page you're? In this case you may use the index of the page

$( '#bb-bookblock-block<?php echo $index; ?>' ).bookblock({settings here});

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wsadfilmAuthor Commented:
It is not a public site, it is a local (intranet) site, so I don’t have a link to provide.  But I will try to provide the code and step through it.

I place the shortcode [BookBlock ‘name of bookblock’] on the page I want the specific book block to show.

EXAMPLE: [BookBlock ‘BookBlock1’]

function shortcodes() {
	$options = $this->get_admin_options();
	add_shortcode('BookBlock', array($this, 'print_shortcode'));
}
function print_shortcode($atts) {
	$options = $this->get_admin_options();
	$BookBlocks = $options['BookBlocks'];
	$title = strtolower($atts[0]);
	
	foreach ($BookBlocks as $BookBlock) {
		if (strtolower($BookBlock['title']) == $title) {
			$slug = $BookBlock['slug'];
		}
	}
	//This creates the HTML needed for the page for the  specific BookBlock
	return $this->get_html($slug);
}

Open in new window

Now I need the script code to be generated on the page for the settings in the footer - this is where I am having issues in passing the name / slug so I end up with all the bookblocks instead of just the one I want.

<? php
add_action('wp_footer', array($BookBlock, 'call_plugin'));

function call_plugin($slug) {
    $options = $this - > get_admin_options();
    $BookBlock = $options['BookBlocks']['BookBlock-1']; ?> < script > (function ($, undefined) {
        $(document).ready(function () { <? php
            foreach($options['BookBlocks'] as $BookBlock) { ?> $('#BookBlock-<?php echo $BookBlock['
                slug ']; ?>').BookBlock({
                    speed: <? php echo $BookBlock['settings']['speed']; ?> ,
                    perspective: <? php echo $BookBlock['settings']['perspective']; ?> ,
                    shadowSides: <? php echo $BookBlock['settings']['shadowSides']; ?> ,
                    shadowFlip: <? php echo $BookBlock['settings']['shadowFlip']; ?> , ...etc.
                }); <? php
            } ?>
        });
    })(jQuery); < /script>
<?php
} ?>

Open in new window

0
 
leakim971PluritechnicianCommented:
so could you post the generated page? (do a right click in your browser and choose view source to post it)
0
 
wsadfilmAuthor Commented:
In the example below, I only want bb1 script to show not both bb1 and bb2

<body>
  <section id="bb-head">
	<ul class="slider" id="bb-bookblock-bb1">
		<li class="bb-item" data-label="Slide1">
			<div class="bbleft">Slide 1</div>
			<div class="bbcenter">Slide 1 data and information here</div>
		</li>
		<li class="bb-item" data-label="Slide2">
			<div class="bbleft">Slide 2</div>
			<div class="bbcenter">Slide 2 data and information here</div>
		</li>
		 <li class="bb-item" data-label="Slide3">
			<div class="bbleft">Slide 3</div>
			<div class="bbcenter">Slide 3 data and information here</div>
		</li>
		<li class="bb-item" data-label="Slide4">
			<div class="bbleft">Slide 4</div>
			<div class="bbcenter">Slide 4 data and information here</div>
		</li>
	</ul>
</section>

<script>
(function ($, undefined) {
    $(document).ready(function () {    
        $('#bb-bookblock-bb1').bookblock({
            perspective: 1500,
            speed: 600,
            shadowSides: 0.5,
            interval: 5000,
            autoplay: true,
            shadowFlip: 0.5
        });
        $('#bb-bookblock-bb2').bookblock({
            perspective: 1500,
            speed: 600,
            shadowSides: 0.5,
            interval: 0,
            autoplay: false,
            shadowFlip: 0.5
        });
    });
})(jQuery);
</script>

</body>

Open in new window

0
 
leakim971PluritechnicianCommented:
so do you know on which page you're ?

for example if you're on page 1 (index is 0) you just need to place an if else in your foreach

and you should reconsiderate what I suggest to you previously to isolate as much as possible javascript to your php code

in the following I only generate javascript variables, not sure all thecode is right but you get the logic

<? php
add_action('wp_footer', array($BookBlock, 'call_plugin'));

function call_plugin($slug) {

    $options = $this - > get_admin_options();
    $BookBlock = $options['BookBlocks']['BookBlock-1'];

    foreach($options['BookBlocks'] as $BookBlock) {
        if( $BookBlock[' slug '] == ("bb".$page_index) ) {
            echo "var php_speed = " . $BookBlock['settings']['speed'] . ";";
            echo "var php_perspective = " . $BookBlock['settings']['perspective'] . ";";
            echo "var php_shadowSides = " . $BookBlock['settings']['shadowSides'] . ";";
            echo "var php_shadowFlip = " . $BookBlock['settings']['php_shadowFlip'] . ";";
            echo "var slug = 'bb'" . $page_index;
        }
    }
}

?>

(function ($, undefined) {
    $(document).ready(function () {    
        $('#bb-bookblock-' + slug).bookblock({
        speed : php_speed,
        perspective : php_perspective,
        shadowSides : php_shadowSide,
        shadowFlip  : php_shadowFlip,
            interval: 5000,
            autoplay: true
        });
    });
})(jQuery);

</script>

Open in new window

0
 
wsadfilmAuthor Commented:
I don’t know how to tell which page I am on what BookBlock is on that page.  Any page could have a BookBlock.  I was creating this plugin to try and make it dynamic where the users could create their own BookBlocks and put them on any page.

Starting to think I bit off more than I could chew on this one – being new to wordpress and plugins.

Am I storing the BookBlock data correctly in wordpress – wp_options or should I look at putting it in the posts – each BookBlock being a custom post instead of them all in the one options record?
0
 
leakim971PluritechnicianCommented:
another solution :

<script type="text/javascript">
<? php
add_action('wp_footer', array($BookBlock, 'call_plugin'));

function call_plugin($slug) {

    $options = $this - > get_admin_options();
    $BookBlock = $options['BookBlocks']['BookBlock-1'];

    foreach($options['BookBlocks'] as $BookBlock) {
            $php_speed[] = $BookBlock['settings']['speed'];
            $php_perspective[] = $BookBlock['settings']['perspective'];
            $php_shadowSides[] = $BookBlock['settings']['shadowSides'];
            $php_shadowFlip[] = $BookBlock['settings']['php_shadowFlip'];
    }
    echo "var php_speed = " . json_encode($speed) . ";";
    echo "var php_perspective = " . json_encode($perspective') . ";";
    echo "var php_shadowSides = " . json_encode($shadowSides') . ";";
    echo "var php_shadowFlip = " . json_encode($php_shadowFlip') . ";";
}

?>

(function ($, undefined) {
    $(document).ready(function () { 

$('ul[id^="bb-bookblock-"]').each(function() {
     var n = $(this).attr("id") * 1 - 1;
     $(this).bookblock({
        speed : php_speed[n],
        perspective : php_perspective[n],
        shadowSides : php_shadowSide[n],
        shadowFlip  : php_shadowFlip[n],
            interval: 5000,
            autoplay: true
        });
});

    });
})(jQuery);
</script>

Open in new window

0
 
wsadfilmAuthor Commented:
Interesting approach.

Thank you for your help
0
 
leakim971PluritechnicianCommented:
line 28 should be (remove not number char) : var n = $(this).attr("id").replace(/\D/g, "") * 1 - 1;
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now