Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <? php
- add_shortcode('example', function( $atts ) {
- //extracting the shortcode attributes
- extract( shortcode_atts( array(
- 'value1' => null,
- 'value2' => null,
- 'value3' => null
- ), $atts ) );
- //Now here I wll do my code for the shortcode
- // But the shortcode needs some js and css to work
- // so, I'm trying like this
- });
- add_action ('wp', 'add_shortcode_script');
- function add_shortcode_script () {
- global $post;
- if ( empty($posts) )
- return $posts;
- $flag = false;
- // setting up regex
- $regex = '/(\w+)\s*=\s*"(.*?)"/';
- // running a check inside posts
- foreach ($posts as $post) {
- // if shortcode found
- if ( stripos($post->post_content, '[EXAMPLE]') ) {
- // set the flag
- $flag = true;
- // then run a preg match scan for the variables inside shortcode
- preg_match_all($regex, $post->post_content, $matches);
- $output = array();
- // running the variable scan inside shortcode
- for ($i = 0; $i < count($matches[1]); $i++) {
- $output[$matches[1][$i]] = $matches[2][$i];
- }
- break;
- }
- }
- if ($flag){
- //using shortcode variables
- $custom_script = 'var a = '. $output[1][value3] .';
- $custom_style = '.class {
- background-color: '. $output[1][value2] .';
- }';
- wp_enqueue_script ('my_script','http:/.../main.js'); // blank js file
- wp_enqueue_style ('my_style','http:/.../main.css'); // blank css file
- wp_add_inline_script ('my_script', $custom_script);
- wp_add_inline_style ('my_style', $custom_stle);
- }
- return $posts;
- }
Add Comment
Please, Sign In to add comment