Last active
April 27, 2017 11:09
-
-
Save igorbenic/bb7e637438ce3ebdbbeb087612e86d31 to your computer and use it in GitHub Desktop.
5 Ways to Make your Plugin Really Extensible | http://ibenic.com/5-ways-make-plugin-extensible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_theme_support( 'my-plugin-style' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // function edd_complete_purchase | |
| do_action( 'edd_complete_purchase', $payment_id ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // class EDD_Cart | |
| // method get_item_price | |
| return apply_filters( 'edd_cart_item_price', $price, $download_id, $options ); | |
| // Only the returning parameter is passed | |
| add_filter( 'edd_cart_item_price', 'change_item_price' ); | |
| function change_item_price( $price ) { | |
| // Do something with the price | |
| // Always return! | |
| return $price; | |
| } | |
| // 2 Parameters | |
| add_filter( 'edd_cart_item_price', 'change_item_price', 20, 2 ); | |
| function change_item_price( $price, $download_id ) {} | |
| // 3 Parameters | |
| add_filter( 'edd_cart_item_price', 'change_item_price', 20, 3 ); | |
| function change_item_price( $price, $download_id, $options ) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function my_plugin_shortcode( $atts ) { | |
| $atts = shortcode_atts( | |
| array( | |
| 'foo' => 'no foo', | |
| 'bar' => 'default bar', | |
| ), | |
| $atts, | |
| 'my_plugin_shortcode' ); // Third Parameter makes a Plugin Extensible | |
| return $output; | |
| } | |
| add_shortcode( 'my_plugin', 'my_plugin_shortcode' ); | |
| // Extending | |
| apply_filter( 'shortcode_atts_my_plugin_shortcode', 'extending_shortcode_atts' ); | |
| function extending_shortcode_atts( $atts ) { | |
| $atts['foo'] = 'Yes Foo'; | |
| return $atts; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function my_plugin_shortcode( $atts ) { | |
| $atts = shortcode_atts( | |
| array( | |
| 'foo' => 'no foo', | |
| 'bar' => 'default bar', | |
| ), | |
| $atts, | |
| 'my_plugin_shortcode' ); // Third Parameter makes a Plugin Extensible | |
| $output = ''; | |
| ob_start(); | |
| ?> | |
| <strong>My Awesome Shortcode</strong> | |
| <?php | |
| do_action( 'my_plugin_shortcode_after_title', $atts ); // Adding an action hook and passing attributes | |
| ?> | |
| <p>Some content</p> | |
| <?php | |
| do_action( 'my_plugin_shortcode_end', $atts ); | |
| $output = ob_get_clean(); | |
| // Enable Filtering | |
| return apply_filters( 'my_plugin_shortcode', $output, $atts ); | |
| } | |
| add_shortcode( 'my_plugin', 'my_plugin_shortcode' ); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'template_include', 'my_plugin_templating' ); | |
| function my_plugin_templating( $template ) { | |
| $file = false; | |
| if( is_singular( 'my-cpt' ) ) { | |
| $file = 'single-my-cpt.php'; | |
| } | |
| if( is_page( $page_id_from_our_settings ) ) { | |
| $file = 'page-my-page.php'; | |
| } | |
| // There is a file, create a new template path | |
| if( $file ) { | |
| // Check in Theme for folder my-plugin/templates/ and the file inside | |
| $template = locate_template( 'my-plugin/templates/' . $file ); | |
| // There is no template in the theme | |
| if( ! $template ) { | |
| // Get from Plugin's folder 'templates' | |
| $template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' . $file; | |
| } | |
| } | |
| return $template; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'wp_enqueue_scripts', 'your_plugin_scripts' ); | |
| function your_plugin_scripts() { | |
| // If the current theme supports it, don't enqueue it | |
| if( ! current_theme_supports( 'my-plugin-style' ) ) { | |
| wp_enqueue_style( 'my-plugin-css', plugins_url( 'assets/plugin.css', __FILE__ ) ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment