From 2bb6dc088a12c626e0e61322210479efeac9a9a8 Mon Sep 17 00:00:00 2001 From: Ned Zimmerman Date: Sun, 4 Feb 2018 16:30:49 -0400 Subject: [PATCH] Add custom shortcodes: aldine_page_section, aldine_call_to_action. --- functions.php | 6 ++++ inc/helpers/namespace.php | 2 +- inc/shortcodes/namespace.php | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 inc/shortcodes/namespace.php diff --git a/functions.php b/functions.php index fb6ec4d..5bec0db 100644 --- a/functions.php +++ b/functions.php @@ -29,6 +29,7 @@ $includes = [ 'customizer', 'filters', 'helpers', + 'shortcodes', 'tags', ]; @@ -52,6 +53,7 @@ add_filter( 'body_class', '\Aldine\Filters\body_classes' ); add_filter( 'excerpt_more', '\Aldine\Filters\excerpt_more' ); add_filter( 'query_vars', '\Aldine\Filters\register_query_vars' ); add_filter( 'wp_nav_menu_items', '\Aldine\Filters\adjust_menu', 10, 2 ); +add_filter( 'the_content', 'do_shortcode' ); add_action( 'widgets_init', '\Aldine\Actions\widgets_init' ); add_action( 'wp_enqueue_scripts', '\Aldine\Actions\enqueue_assets' ); add_action( 'updated_option', '\Aldine\Actions\add_color_variants', 10, 3 ); @@ -59,6 +61,10 @@ add_action( 'customize_register', '\Aldine\Customizer\customize_register' ); add_action( 'customize_preview_init', '\Aldine\Customizer\customize_preview_js' ); add_action( 'customize_controls_enqueue_scripts', '\Aldine\Customizer\enqueue_color_contrast_validator' ); +// Shortcodes +add_shortcode( 'aldine_page_section', '\Aldine\Shortcodes\page_section' ); +add_shortcode( 'aldine_call_to_action', '\Aldine\Shortcodes\call_to_action' ); + // Catalog page: Network admin controls add_action( 'admin_enqueue_scripts', '\Aldine\Admin\admin_scripts' ); add_action( 'wp_ajax_pressbooks_aldine_update_catalog', '\Aldine\Admin\update_catalog' ); diff --git a/inc/helpers/namespace.php b/inc/helpers/namespace.php index 8764688..d32eb7f 100644 --- a/inc/helpers/namespace.php +++ b/inc/helpers/namespace.php @@ -308,7 +308,7 @@ function handle_contact_form_submission() { function has_sections( $post_id ) { $post_content = get_post_field( 'post_content', $post_id ); if ( ! empty( $post_content ) ) { - if ( strpos( $post_content, 'page-section' ) ) { + if ( strpos( $post_content, 'page-section' ) || strpos( $post_content, 'aldine_page_section' ) ) { return true; } else { return false; diff --git a/inc/shortcodes/namespace.php b/inc/shortcodes/namespace.php new file mode 100644 index 0000000..455513a --- /dev/null +++ b/inc/shortcodes/namespace.php @@ -0,0 +1,58 @@ + 'Page Section', + 'variant' => '', + ], + $atts, + 'aldine_page_section' + ); + + return sprintf( + '

%2$s

%3$s
', + ( $atts['variant'] ) ? " page-section--{$atts['variant']}" : '', + $atts['title'], + $content + ); +} + +/** + * Shortcode for custom Call to Action. + * + * @param array $atts + * + * @return string + */ +function call_to_action( $atts ) { + $atts = shortcode_atts( + [ + 'link' => '#', + 'text' => 'Call To Action', + ], + $atts, + 'aldine_call_to_action' + ); + + return sprintf( + '%2$s', + $atts['link'], + $atts['text'] + ); +}