Last active
November 2, 2018 14:53
-
-
Save jrfoell/363ad0b36f83b63ea64921f119606f9d to your computer and use it in GitHub Desktop.
Register Custom Beaver Builder Module and Custom Field
This file contains 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 | |
/** | |
* Posts Category List class and registration call. | |
* | |
* @package WDS_BB_Custom_Field | |
* @since 1.0 | |
*/ | |
/** | |
* Post Category List Module. | |
* | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
class Post_Category_List_Module extends FLBuilderModule { | |
/** | |
* Default post count limit. | |
* | |
* @since 1.0 | |
*/ | |
const POST_COUNT_DEFAULT = 5; | |
/** | |
* Module constructor. | |
* | |
* @see https://kb.wpbeaverbuilder.com/article/597-cmdg-02-add-a-module-to-your-plugin | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
public function __construct() { | |
parent::__construct( | |
array( | |
'name' => esc_html__( 'Post Category List', 'wds-bb-custom-field' ), | |
'description' => esc_html__( 'A post category list module.', 'wds-bb-custom-field' ), | |
'group' => esc_html__( 'WDS Modules', 'wds-bb-custom-field' ), | |
'category' => esc_html__( 'Posts', 'wds-bb-custom-field' ), | |
'dir' => plugin_dir_path( __FILE__ ), | |
'url' => plugins_url( dirname( __FILE__ ) ), | |
) | |
); | |
} | |
/** | |
* Set up a WP_Query based on our module's settings. | |
* | |
* @return WP_Query | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
public function category_posts_query() { | |
return new WP_Query( array( | |
'posts_per_page' => $this->settings->post_count, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'category', | |
'field' => 'id', | |
'terms' => $this->settings->post_categories, | |
), | |
), | |
) ); | |
} | |
} | |
/** | |
* Register the module and its form settings. | |
* | |
* @see https://kb.wpbeaverbuilder.com/article/598-cmdg-03-define-module-settings | |
* @since 1.0 | |
*/ | |
FLBuilder::register_module( | |
'Post_Category_List_Module', array( | |
'general' => array( | |
'title' => esc_html__( 'General', 'wds-bb-custom-field' ), | |
'sections' => array( | |
'general' => array( | |
'fields' => array( | |
'list_title' => array( | |
'type' => 'text', | |
'label' => esc_html__( 'List Title', 'wds-bb-custom-field' ), | |
'default' => '', | |
'placeholder' => esc_html__( 'Enter a title or leave blank to omit', 'wds-bb-custom-field' ), | |
), | |
'post_count' => array( | |
'type' => 'unit', | |
'label' => esc_html__( 'Number of Posts', 'wds-bb-custom-field' ), | |
'default' => Post_Category_List_Module::POST_COUNT_DEFAULT, | |
), | |
'post_categories' => array( | |
'type' => 'wds-bb-post-categories', | |
'label' => esc_html__( 'Categories to Include', 'wds-bb-custom-field' ), | |
), | |
), | |
), | |
), | |
), | |
) | |
); |
This file contains 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 | |
/** | |
* The main class for our plugin. | |
* | |
* @package WDS_BB_Custom_Field | |
* @since 1.0 | |
*/ | |
/** | |
* Plugin class. | |
* | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
class WDS_BB_Custom_Field_Plugin { | |
/** | |
* Hook it up! | |
* | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
public function register_hooks() { | |
// Load custom modules. | |
add_action( 'init', array( $this, 'load_modules' ) ); | |
// Register custom fields. | |
add_filter( 'fl_builder_custom_fields', array( $this, 'register_fields' ) ); | |
} | |
/** | |
* Load a custom module. | |
* | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
public function load_modules() { | |
require_once WDS_BB_CUSTOM_FIELD_DIR . 'include/modules/post-category-list/class-post-category-list-module.php'; | |
} | |
/** | |
* Register custom field(s) for our module. | |
* | |
* @param array $fields Beaver Builder fields - index is the field name, value is the file path. | |
* @return array $fields Beaver Builder fields with additional custom fields. | |
* @author Justin Foell <justin.foell@webdevstudios.com> | |
* @since 1.0 | |
*/ | |
public function register_fields( $fields ) { | |
$fields['wds-bb-post-categories'] = WDS_BB_CUSTOM_FIELD_DIR . 'include/fields/post-categories.php'; | |
return $fields; | |
} | |
} |
This file contains 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 | |
/** | |
* Custom field to select Post Categories. | |
* | |
* @see https://kb.wpbeaverbuilder.com/article/620-cmdg-14-create-custom-fields | |
* @since 1.0 | |
* @package WDS_BB_Custom_Field | |
*/ | |
foreach ( get_categories() as $category ) : | |
?> | |
<div> | |
<label> | |
<# | |
// debugger; | |
var checked = ''; | |
if ( 'object' === typeof data.value && jQuery.inArray( '<?php echo esc_attr( $category->term_id ); ?>', data.value ) != -1 ) { | |
checked = ' checked="checked"'; | |
} | |
#> | |
<input type="checkbox" name="{{data.name}}[]" value="<?php echo esc_attr( $category->term_id ); ?>"{{{checked}}}/> | |
<?php echo esc_html( $category->name ); ?> | |
</label> | |
</div> | |
<?php | |
endforeach; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment