Guest User

Untitled

a guest
Aug 14th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <? php
  2. add_shortcode('example', function( $atts ) {
  3.     //extracting the shortcode attributes
  4.     extract( shortcode_atts( array(
  5.         'value1' => null,
  6.         'value2' => null,
  7.         'value3' => null
  8.     ), $atts ) );
  9.  
  10.     //Now here I wll do my code for the shortcode
  11.     // But the shortcode needs some js and css to work
  12.     // so, I'm trying like this
  13.  
  14. });
  15.  
  16. add_action ('wp', 'add_shortcode_script');
  17.  
  18. function add_shortcode_script () {
  19.   global $post;
  20. if ( empty($posts) )
  21.         return $posts;
  22.  
  23.     $flag = false;
  24.  
  25.     foreach ($posts as $post) {
  26.         if ( stripos($post->post_content, '[EXAMPLE') )
  27.             $flag = true;
  28.             break;
  29.         }
  30.  
  31.     if ($flag){
  32.  
  33. $custom_script = '<script type="text/javascript">
  34.            var a = '. $value3 .'
  35.        </script>';
  36.  
  37. $custom_style = '<style type="text/css">
  38.            .class {
  39.                background-color: '. $value2 .';
  40.            }
  41.        </style>';
  42.  
  43. // But I'm not sure how you propose to fetch the shortcode variables like $value2, $value3 here
  44. wp_add_inline_script ('my_script', $custom_script);
  45.   wp_add_inline_style ('my_style', $custom_stle);
  46.     }
  47.     return $posts;
  48. }
Add Comment
Please, Sign In to add comment