Customize meta box through child theme - php

I am using penny auction wordpress theme, as i am new to wordpress. My question is to add more fields in side meta-box by using child theme, So for this I have successfully created and activated child theme, but I am stuck here to how can I add more text fields in meta box by using child theme functions file?

The easiest way to do that is to install the Meta Box plugin (https://wordpress.org/plugins/meta-box/), then add this to your child theme's functions.php file:
function yourprefix_register_meta_boxes( $meta_boxes ) {
$prefix = 'metaboxprefix_';
$meta_boxes[] = array(
'id' => 'samplefield',
'title' => __( 'Your Meta Box Title', 'yourtextdomain' ),
'post_types' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __( 'Your Text Field', 'yourtextdomain' ),
'id' => $prefix . 'yourfieldid',
'type' => 'text',
),
)
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'yourprefix_register_meta_boxes' );
For future reference: http://metabox.io/

Related

Metabox display specifically in Home Page

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!

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;
}

How to add custom fields to customize menu in Wordpress

I want to add some custom menu points to the customize menu. So the user is able to edit the page much easier.
In your theme go to the function.php
and Code like this
function self_customizer_section($wp_customize) {
$wp_customize->add_section( 'section_name' , array(
'title' => __( 'Self Logo', 'my_theme' ),
'description' => 'theme general options',
));
/* LOGO */
$wp_customize->add_setting( 'self_logo', array(
'default' => get_template_directory_uri().'/images/mytheme.png'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'self_logo', array(
'label' => __( 'Main Logo', 'my_theme' ),
'section' => 'section_name',
'settings' => 'self_logo',
)));
}
add_action('customize_register', 'self_customizer_section');
For more detail you can follow the below link he made the awesome tutorial on that question
See in continuation.
here is the link
Wordpress Theme Customization API Tutorial

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,
) );
}

Shortcodes are not working in custom metabox

I am having issue with WordPress meta boxes. actually I am using WordPress Genesis framework and in child theme I had create few meta boxes for my client to show some content before the page content, but in the custom meta box i am using wp-editor and its working fine. but the issue is that when I try to use some shortcodes in this wp-editor then it won't show me anything, it is just returning the whole shortcode as it is.
I am using https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress for custom meta boxes.
And my codes are in function.php file:
/* -------------------------------------------------------------------------- */
/* Setup Custom metaboxes */
/* -------------------------------------------------------------------------- */
add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( CHILD_DIR . '/lib/metabox/init.php' );
}
}
add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
function cmb_sample_metaboxes( array $meta_boxes ) {
// Start with an underscore to hide fields from custom fields list
$prefix = '_cmb_';
$meta_boxes[] = array(
'id' => 'text_content',
'title' => 'Text Content',
'pages' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Custom Content',
'desc' => 'This is a title description',
'id' => $prefix . 'custom_content',
'type' => 'title',
),
array(
'name' => 'Tab Name',
'desc' => 'Please descibe the tab name (required)',
'id' => $prefix . 'tab_name',
'type' => 'text',
),
array(
'name' => 'Test wysiwyg',
'desc' => 'field description (optional)',
'id' => $prefix . 'test_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}
I save the codes in page.php as:
add_action('genesis_before_loop', 'ehline_before_loop_content');
function ehline_before_loop_content()
{
echo genesis_get_custom_field( '_cmb_tab_name' );
echo '<br />';
echo genesis_get_custom_field( '_cmb_test_wysiwyg' );
}
genesis();
But when I use the shortcodes in this meta box it return something like that
[wptabtitle] Tab 01[/wptabtitle] [wptabcontent]test[/wptabcontent]
Please anyone tell me how can I make it to use shortcodes in the wp-editor.
You need to call do_shortcode() for the content of your custom fields. Here is how the updated code should look like:
add_action('genesis_before_loop', 'ehline_before_loop_content');
function ehline_before_loop_content()
{
echo do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) );
echo '<br />';
echo do_shortcode( genesis_get_custom_field( '_cmb_test_wysiwyg' ) );
}
genesis();
Also this will not add the auto-paragraphs that you would usually see for your posts contents. You can do two things:
echo apply_filters( 'the_content', genesis_get_custom_field( '_cmb_tab_name' ) );
or
echo wpautop( do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) ) );
In theory the first one should be better, but sometimes you might get additional output from functions that hook to the the_content filter.

Categories