/
/ Includes /
shortcodes.php
...
functions.php
function foobar_func( $atts ) { return "foo and bar"; } add_shortcode( 'foobar', 'foobar_func' ); function foo_script () { wp_register_script( 'foo-js', get_template_directory_uri() . '/includes/js/foo.js'); wp_enqueue_script( 'foo-js' ); } add_action( 'wp_enqueue_scripts', 'foo_script');
class foobar_shortcode { static $add_script; static function init () { add_shortcode('foobar', array(__CLASS__, 'foobar_func')); add_action('init', array(__CLASS__, 'register_script')); add_action('wp_footer', array(__CLASS__, 'print_script')); } static function foobar_func( $atts ) { self::$add_script = true; return "foo and bar"; } static function register_script() { wp_register_script( 'foo-js', get_template_directory_uri() . '/includes/js/foo.js'); } static function print_script () { if ( !self::$add_script ) return; wp_print_scripts('foo-js'); } } foobar_shortcode::init();
[price]
- [plan title = 'Plan 1' price = '99 ']
- [option] Option 1 [/ option]
- [option] Option 2 [/ option]
- [option] ... [/ option]
- [/ plan]
- [plan title = 'Plan 2' price = '499']
- [option] Option 1 [/ option]
- [option] Option 2 [/ option]
- [option] ... [/ option]
- [/ plan]
...
[/ price]
add_shortcode ('price', 'price_code');
add_shortcode ('plan', 'plan_code');
add_shortcode ('option', 'option_code');
function price_code ($atts, $content) { // $GLOBALS['plan-count'] = 0; $GLOBALS['plans'] = array(); // do_shortcode($content); // HTML $output = '<div class="price">'; if(is_array($GLOBALS['plans'])) { foreach ($GLOBALS['plans'] as $plan) { $planContent = '<div class="plan">'; $planContent .= $plan; $planContent .= '</div>'; $output .= $planContent; } } $output .= '</div>'; // HTML return $output; }
function plan_code ($atts, $content) { // extract(shortcode_atts(array( 'title' => '', // Plan title name 'price' => '0', // Plan price ), $atts)); // HTML: $plan_title = '<div class="plan-title">'; $plan_title .= ' <p>'.$title.'</p>'; $plan_title .= '</div>'; // HTML: $f_price = round(floatval($price), 2); $f_price = ($f_Price > 0) ? $f_Price : 0; $s_price = '$'.$f_Price; $price_plan = '<div class="plan">'; $price_plan .= ' <p class="price-sum">'.$s_price.'</p>'; $price_plan .= ' <small class="price-text">'.$text.'</small>'; $price_plan .= '</div>'; // $GLOBALS['plan-options-count'] = 0; $GLOBALS['plan-options'] = array(); // do_shortcode($content); // HTML: $plan_options = '<div class="plan-options">'; if (is_array($GLOBALS['plan-options'])) { foreach ($GLOBALS['plan-options'] as $option) { $plan_options .= $option; } } $s_OptionsDiv.= '</div>'; // HTML: $plan_div = $plan_title; $plan_div .= $price_plan; $plan_div .= $plan_options; // $i = $GLOBALS['plan-count'] + 1; $GLOBALS['plans'][$i] = $plan_div; $GLOBALS['plan-count'] = $i; // return true; } function option_code ($atts, $content) { // HTML $plan_option = '<div class="price-option">'; $plan_option .= ' <p class="price-option-text">'.do_shortcode($content).'</p>'; $plan_option .= '</div>'; // $i = $GLOBALS['plan-options-count'] + 1; $GLOBALS['plan-options'][$i] = $plan_option; $GLOBALS['plan-options-count'] = $i; // return true; }
[column_half]
[column_half] Content [/ column_half]
[column_half] Content [/ column_half]
[/ column_half]
[column_half] Content [/ column_half]
add_shortcode( 'column_half', 'column_half_code' ); add_shortcode( 'column_half_inner', 'column_half_code' ); function column_half_code ( $atts, $content ) { return "<div class='col-lg-6'>".do_shortcode($content)."</div>"; } : [column_half] [column_half_inner] Content [/column_half_inner] [column_half_inner] Content [/column_half_inner] [/column_half] [column_half] Content [/column_half]
Source: https://habr.com/ru/post/265245/
All Articles