Metabox display specifically in Home Page - php

I'm using Metabox Plugin in WP and generate a field using MB online generator.
add_filter( 'rwmb_meta_boxes', 'bq_homemeta_register_meta_boxes' );
function bq_homemeta_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'Homepage Meta', 'bequik' ),
'id' => 'home_meta',
'context' => 'normal',
'pages' => 'page',
'fields' => [
[
'type' => 'image_advanced',
'name' => esc_html__( 'Hero Image', 'bequik' ),
'id' => $prefix . 'hero_image',
'max_file_uploads' => 1,
],
],
];
return $meta_boxes;
}
I wanted the metabox field to display only in home page in the WP admin editor.
I tried the include/exclude extension but failed to work.
Thank you!

Related

Creating page and post specific ACF Gutenberg blocks

I'm creating custom ACF Gutenberg blocks my a site and have successfully managed to register my own blocks. Now, I have a custom post type called Blog. I do not want blog to show all my ACF Gutenberg blocks, I want to create a separate batch for custom post type use only. I have enabled show_in_rest, but even the default Gutenberg blogs do not show for me?
Here is my approach:
1. Registering the post type (theme/functions.php)
<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
'show_in_rest' => true,
'menu_icon' => 'dashicons-edit',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array(
'editor',
'title',
'author',
'revisions',
'excerpt',
'thumbnail'
) ,
)));
?>
2. Registering the ACF Gutenberg blocks for pages (theme/inc/acf-blocks/blocks.php)
Here are the blocks that I've registered for use on pages (not on the blog post type):
<?php
$hero = array(
'name' => 'hero',
'title' => __('Hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
) ,
);
$blocks = [$hero];
return $blocks;
?>
Registering the ACF Gutenberg blocks for blog post type (theme/inc/acf-blocks/blog-blocks.php)
<?php
$blog_hero = array(
'name' => 'blog_hero',
'title' => __('Blog hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
) ,
);
$blog_blocks = [$blog_hero];
return $blog_blocks;
?>
Register all blocks (theme/inc/acf-blocks/functions.php)
<?php
/*
* loop though array and register each block type
*/
function block_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blocks.php';
$blocks = require($path);
foreach($blocks as $block) {
acf_register_block_type($block);
}
}
function blog_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
$blog_blocks = require($path);
foreach($blog_blocks as $blog_block) {
acf_register_block_type($blog_block);
}
}
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'block_acf_init');
add_action('acf/init', 'blog_acf_init');
}
?>
Current results:
When creating a post on the blog custom post type, I do not have the ability to add any blocks, let alone see if blog_hero block appears:
On pages, I can see all my created blocks, however, the blog hero block shows on the page side, when I only want it for the custom post type:
Probably this way could solve the problem:
Specifying post_types param for Blog Hero block to blog
$blog_hero = array(
'name' => 'blog_hero',
'title' => __( 'Blog hero', 'Context' ),
'description' => __( '', 'Context' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
),
'post_types' => array( 'blog' ),
);
And analogically specifying all post types except blog for Hero block.
$all_post_types = get_post_types();
$hero_block_post_types = array_diff( $all_post_types, array( 'blog' ) );
$hero = array(
'name' => 'hero',
'title' => __( 'Hero', 'Domain' ),
'description' => __( '', 'Domain' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
),
'post_types' => $hero_block_post_types
);
$blocks = [ $hero ];
return $blocks;
Notice:
Consider adding Domain for your __ function.
Good practice to use __ function with Domain.

Adding subtitles to products in Woocommerce

I'd like to be able to add a subtitle to my products in Woocommerce only on the shop page. I have it set up as a catalog right now, since I'm not actively selling my products yet, so I have disabled the add to cart button and the prices. I would like to be able to say how many colors the product comes in as a subtitle. I have read up on adding meta boxes with Woocommerce, and found this solution and added it to my functions.php, but it doesn't appear to be working on my site (I can't find where to enter the information for the product!)
add_filter( 'cmb_meta_boxes', 'bhww_core_cpt_metaboxes' );
function bhww_core_cpt_metaboxes( $meta_boxes ) {
//global $prefix;
$prefix = '_bhww_'; // Prefix for all fields
// Add metaboxes to the 'Product' CPT
$meta_boxes[] = array(
'id' => 'bhww_woo_tabs_metabox',
'title' => 'Additional Product Information - <strong>Optional</strong>',
'pages' => array( 'product' ), // Which post type to associate with?
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Colors', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Colors tab.', 'cmb' ),
'id' => $prefix . 'ingredients_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}

Adding new Icons to Visual Composer

I am trying to add a new font icon set to Visual Composer and although the name is appearing in the dropdown; No dropdown for the actual font icons are being loaded and I cannot figure out what's missing or wrong with my code below.
Any help would be much appreciated.
// Add new custom font to Font Family selection in icon box module
function reach_add_new_icon_set_to_iconbox( ) {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][__( 'Reach Icons', 'reach-rdp' )] = 'reach_icons';
vc_update_shortcode_param( 'vc_icon', $param );
}
add_filter( 'init', 'reach_add_new_icon_set_to_iconbox', 40 );
function reach_add_font_picker() {
vc_add_param( 'vc_icon', array(
'type' => 'iconpicker',
'heading' => esc_html__( 'Icon', 'reach-rdp' ),
'param_name' => 'icons_reach_icons',
'settings' => array(
'emptyIcon' => false,
'type' => 'reach_icons',
'iconsPerPage' => 20,
),
'dependency' => array(
'element' => 'icon_type',
'value' => 'reach_icons',
),
'group' => esc_html__( 'Icon', 'reach-rdp' ),
)
);
}
add_filter( 'vc_after_init', 'reach_add_font_picker', 40 );
function reach_vc_iconpicker_type_reach_icons( $icons ) {
// Add custom icons to array
$icons['Reach Icons'] = array(
array( "icon-arrow-left" => "Arrow Left" ),
);
// Return icons
return $icons;
}
add_filter( 'vc_iconpicker-type-reach_icons', 'reach_vc_iconpicker_type_reach_icons' );
/**
* Register Backend and Frontend CSS Styles
*/
add_action( 'vc_base_register_front_css', 'leadinjection_vc_iconpicker_base_register_css' );
add_action( 'vc_base_register_admin_css', 'leadinjection_vc_iconpicker_base_register_css' );
function leadinjection_vc_iconpicker_base_register_css(){
wp_register_style('reach_icons', get_stylesheet_directory_uri() . '/assets/css/reach-font.css');
}
/**
* Enqueue Backend and Frontend CSS Styles
*/
add_action( 'vc_backend_editor_enqueue_js_css', 'leadinjection_vc_iconpicker_editor_jscss' );
add_action( 'vc_frontend_editor_enqueue_js_css', 'leadinjection_vc_iconpicker_editor_jscss' );
function leadinjection_vc_iconpicker_editor_jscss(){
wp_enqueue_style( 'reach_icons' );
}
It looks like you do the most things correct, you just need to replace:
add_filter('init....)
to:
add_action('vc_after_init',....)
update:
Also you have a wrong param name in dependency.
it should be:
'element' => 'type',
And I also will recommend to use the weight attribute to make sorting better:
function reach_add_new_icon_set_to_iconbox( ) {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][__( 'Reach Icons', 'reach-rdp' )] = 'reach_icons';
$param['weight'] = 90;
vc_update_shortcode_param( 'vc_icon', $param );
}
and
function reach_add_font_picker() {
vc_add_param( 'vc_icon', array(
'type' => 'iconpicker',
'heading' => esc_html__( 'Icon', 'reach-rdp' ),
'param_name' => 'icons_reach_icons',
'settings' => array(
'emptyIcon' => false,
'type' => 'reach_icons',
'iconsPerPage' => 20,
),
'weight' => 80,
'dependency' => array(
'element' => 'type',
'value' => 'reach_icons',
),
)
);
}
I needed to achieve exactly the same thing - although the icon set I'm adding is selection of icons from the Font Awesome Pro set. Using the answer that almost worked above, here is my fully working version. This is tested and working in version 5.5.2 of WPBakery. I hope this helps someone!
// In the 'Icon library' dropdown for an icon content type, add a new family of icons.
function fapro_add_to_iconbox() {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][ __( 'FontAwesome Pro icons', 'js_composer' ) ] = 'fapro';
vc_update_shortcode_param( 'vc_icon', $param );
}
add_filter( 'vc_after_init', 'fapro_add_to_iconbox', 40 );
// This adds a new parameter to the vc_icon content block.
// This parameter is an icon_picker element,
// that displays when fapro is picked from the dropdown.
function fapro_add_font_picker() {
$settings = array(
'type' => 'iconpicker',
'heading' => __( 'Icon', 'js_composer' ),
'param_name' => 'icon_fapro',
'settings' => array(
'emptyIcon' => false,
'type' => 'fapro',
'iconsPerPage' => 20,
),
'dependency' => array(
'element' => 'type',
'value' => 'fapro',
),
'weight' => '1',
'description' => __( 'Select icon from library.', 'js_composer' ),
);
vc_add_param( 'vc_icon', $settings );
}
add_filter( 'vc_after_init', 'fapro_add_font_picker', 50 );
// Add all the icons we want to display in our font family.
function vc_iconpicker_type_fapro( $icons ) {
// Add custom icons to array.
$fapro_icons = array(
array( 'far fa-bacon' => 'Bacon' ),
array( 'fas fa-hamburger' => 'Hamburger' ),
);
// Return icons.
return $fapro_icons;
}
add_filter( 'vc_iconpicker-type-fapro', 'vc_iconpicker_type_fapro' );
// Enqueue the CSS file so that the icons display in the backend editor.
function enqueue_fapro_font() {
wp_enqueue_style( 'agilechilli-font-awesome', 'https://pro.fontawesome.com/releases/v5.7.2/css/all.css' );
}
add_action( 'vc_backend_editor_enqueue_js_css', 'enqueue_fapro_font' );

How to include excerpt option on taxonomy category page?

I am trying to add an excerpt option to my category page, to display instead of my description.
So basically, I need a box on this screen which will be able to be used as preview text.
The code used to create this taxonomy is:
add_action( 'init', 'create_product_cat_external' );
function create_product_cat_external() {
register_taxonomy(
'ExternalProducts',
'products',
array(
'label' => __( 'External Products' ),
'rewrite' => array( 'slug' => 'externalproducts' ),
'hierarchical' => true,
)
);
}
and the box needs to be here:
You can use CMB2 plugin and put this in your functions.php for example:
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
function yourprefix_register_taxonomy_metabox() {
$prefix = 'yourprefix_term_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'edit',
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'products'), // Tells CMB2 which taxonomies should have these fields
// 'new_term_section' => true, // Will display in the "Add New Category" section
) );
$cmb_term->add_field( array(
'name' => __('Excerpt', 'default'),
'id' => $prefix . 'excerpt',
'type' => 'wysiwyg',
'on_front' => false,
) );
}

How to have more than one "Theme Options" page with OptionTree?

OptionTree (GitHub) allows to create a "Theme Options" page for themes very simply.
How could I extend OptionTree in order to create a "Plugin Options" page for my plugin?
Thank you!
It's actually pretty simple. The following code will create a page under the settings page called Test Page. This is how OptionTree creates its own pages.
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
}
}

Categories