This is a php question, that happens to involve Wordpress.
My Wordpress parent theme adds a custom metabox. I need to add additional options to the metabox but I need to add the key values options from within my child theme...not in the parent theme where the array is created.
Adding the additional options (key values) in the parent theme is simple, but core theme files have to be changed and therefore will break when the partent theme is updated by the theme developer.
So I have made a child theme that handles all my custom adds...but I have no clue how to inject key vales into an array that is crated in the parent theme.
Here is an excerpt from the the array that builds the options for the metabox (parent theme):
$page_meta_boxes = array(
"Page Item" => array(
'item'=>'page-option-item-type' ,
'size'=>'page-option-item-size',
'xml'=>'page-option-item-xml',
'type'=>'page-option-item',
'name'=>array(
'home-page-featured'=>array(
'main-title'=>array(
'title'=> 'MAIN TITLE',
'name'=> 'page-option-item-featured-text-title',
'type'=> 'inputtext'),
'main-caption'=>array(
'title'=> 'MAIN CAPTION',
'name'=> 'page-option-item-stunning-text-caption',
'type'=> 'textarea'),
),
)
),
)
I want to add additional options to the metabox such as:
'get-started-button-title'=>array(
'title'=> 'GETTING STARTED BUTTON TITLE',
'name'=> 'page-option-item-featured-text-button-title',
'type'=> 'inputtext',
'description'=> 'The stunning text button will appear if this field is not a blank.'),
'get-started-button-link'=>array(
'title'=> 'GETTING STARTED BUTTON LINK',
'name'=> 'page-option-item-featured-text-button-link',
'type'=> 'inputtext',
'description'=> 'This is a stunning text button link url. This field will be ignored when button title equals to blank.'),
Is this even possible?
UPDATE:
What I have tried so far
I am including a file called options.php from within my child theme but its not adding the additional getting-started-button option.
$page_meta_boxes['Page Item']['name']['home-page-featured']['get-started-button-title'] = array(
'title'=> 'GETTING STARTED BUTTON LINK',
'name'=> 'page-option-item-featured-text-button-link',
'type'=> 'inputtext',
'description'=> 'This is a stunning text button link url. This field will be ignored when button title equals to blank.'
);
You could add additionnal options like this :
$page_meta_boxes['Page Item']['name']['home-page-featured']['get-started-button-title'] = array(....);
Be aware that even if you don't change the core theme. You rely on the way it is structured. So your code might still break in case of an update by the theme developer.
Related
We have a home page banner in our WordPress website. Added a logic to show different images on page refresh.
The code for the images are placed in the help_functions.php, updated the file and it worked as expected.
$images = array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
$rand_img = array_rand($images, 1);
$image =$images[$rand_img];
Instead of hardcoding, I register a new custom widget by adding below code in the functions.php
register_sidebar(array(
'name' => esc_html__('Splash images Widget', 'wpresidence'),
'id' => 'splashimage-widget-area',
'description' => esc_html__('The splash image widget area', 'wpresidence'),
'before_widget' => ' ',
'after_widget' => ' ',
'before_title' => ' ',
'after_title' => ' ',
));
add_action( 'widgets_init', 'register_my_widgets' );
and I added the custom html to the widget and added the below code
array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
And tried updating the code in help_functions.php file as below
$images = dynamic_sidebar( 'splashimage-widget-area' );
$rand_img = array_rand($images, 1);
$header_type=20;
$image =$images[$rand_img];
Its not working as expected.
Expected result, Add images link in the widget and update the help_functions.php file to show the different images on refresh.
I register a new custom widget
You are registering an area for the widgets using register_sidebar(), not the widget itself.
This widget area allows you to assign widgets (custom HTML, image, recent posts etc) into that area and specify where you want them to appear in your template files.
To register a widget you need to use register_widget() function and class, see this tutorial on how to create your first widget.
However, if you don't want to register new widget you can register shortcode that outputs what you want, then add it via a custom html/text widget.
Registering shortcode is very easy using add_shortcode() function, especially if you plan to hardcode an array of images and not pass any attributes.
More on registering shortcode here.
I am collecting an additional User Meta Data for my Woo commerce check out page.
woocommerce_form_field('myName', array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('First Name'),
'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myName'));
And I am updating to the database with this code:
/*Update the info with the checkout*/
add_action('woocommerce_checkout_field_update_order_meta','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta($order_id){
if($_POST['MyName'])
update_post_meta($order_id, 'First Name',esc_attr($POST['MyName']));
}
Each time I submit, I get an internal server Error, even though I am working on a local machine. I need to collect that data and persist it into the order Database. Anybody help?
Update: Normally the postmeta meta_key should not use white spaces and capitals…
Also your additional field should need to be inside the checkout form, if not nothing will be submitted and nothing can be saved.
To display an custom text field and save it in the database once submitted, the best way is to use the following:
// Add checkout custom text field
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_field', 20, 1 );
function add_checkout_custom_field( $checkout) {
// Text field
woocommerce_form_field('my_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('First Name'),
'placeholder' =>__('Please enter your name'),
), $checkout->get_value('my_name') );
}
// Save the data to the order
add_action('woocommerce_checkout_create_order','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta( $order ){
if( isset($_POST['my_name']) && ! empty($_POST['my_name']) )
$order->update_meta_data( '_my_name', sanitize_text_field($POST['my_name']) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works…
To get this data once saved in the order, use get_post_meta( $order_id, '_my_name', true ); where $order_id is the dynamic order ID…
Your additional text field will be located just before "Order notes" field:
Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.
I am working on a WordPress website, with WooCommerce functionality.
I have created 2 Custom Fields, for the Product Data box within the backend of the Product Page, using the following Code:
<?php
function theme_name_woocommerce_custom_fields() {
// Price Per Character
woocommerce_wp_text_input(
array(
'id' => '_character_price',
'label' => 'Price Per Character',
'description' => 'This is the amount a customer pays per letter entered.',
'desc_tip' => 'true',
'placeholder' => 'Enter Amount per Letter (Exclude the £)'
)
);
// Custom Text Box
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_box',
'label' => 'Show Custom Text Box',
'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
'desc_tip' => 'true',
)
);
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );
function save_theme_name_woocommerce_custom_field( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>
In reference to the woocommerce_wp_checkbox, I would like to create a function whereby when this checkbox is selected, it creates a Custom Text Box on the associated Product's page. This Custom Text Box can then be used by a potential customer to enter a piece of text, which they would like to have printed to the page's Product.
Is anyone aware of what additional piece of coding I would need to enter, in order to achieve said goal?
You can override simple/variable product's template file and add custom field there. Keep logic of show/hide textbox when checkbox is checked there only.
When add to cart is done, you will get all posted data in POST. Grab it from there and put into woocommerce' object.
=======================================================
Edited:
You can follow this steps:
Copy woocommerce/templates/single-product/add-to-cart/variable.php this template to your child theme and customize it. You will see a form tag there. In that you can put your custom created checkbox(which you added from admin). Also, add a custom textbox(in which user will enter text) - and hide it.
Add custom js from functions.php. - In this js you can write logic that if checkbox is checked then you will show the textbox, else not.
Now, when user does add to cart, add this custom textbox data to woocommerce object. How to enter custom data to woocommerce object - you can have step by step details here: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/
Repeater field plugin is purchased in Redux framework. But I am creating it as a custom filed .
I have created 3 fields(title, phone, Textarea) with add more and delete button. In this, I want to add upload button along with that three fields . But I failed to add upload button . I tried a lot but it's not working .
Can you please tell me what is the way to add upload image button in custom field extension.
$fields = array(
'id'=>'multi-text',
'type' => 'multi_text',
'title' => __('Multi Text Option - Color Validated', 'textdomain'),
'validate' => 'color',
'subtitle' => __('If you enter an invalid color it will be removed. Try using the text "blue" as a color. ;)', 'texdoma'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo')
),
);
Example Usage
This example in based on the example usage provided above. Be sure to change $redux_demo to the value you specified in your opt_name argument.
global $redux_demo;
echo 'First Text Entry: ' . $redux_demo['multi-text'][0];
// The array number of additional entries will increase by one.
I register a Custom Post Type, and I don't want it to have its own menu, instead I want to place it as a submenu of an existing admin menu item called my-custom-parent-page.
Here's my code:
register_post_type('my_custom_post_type',
array(
'labels' => array(
'name' => __('Books', 'mcpt'),
'singular_name' => __('Book', 'mcpt'),
),
'supports' => array('title', 'editor'),
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'my-custom-parent-page',
)
);
It works, meaning that it's properly located under the menu my-custom-parent-page, however now when I click on the parent menu (i.e. my-custom-parent-page) it points me to the my_custom_post_type page...
Any help?
Place a Custom-Post-Type in an submenu of an existing parent page
According to the Codex, this is a known and expected behavior:
Note: When using 'some string' to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top level link.
Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the "show_in_menu" section)
Here is the end of the quote which offers a solution:
If this isn't desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.
So this is quite simple to solve. However in my case I couldn't change the priority of the parent page because it is generated by a third-party library. Therefore I came up with this solution:
// Move the "example_cpt" Custom-Post-Type to be a submenu of the "example_parent_page_id" admin page.
add_action('admin_menu', 'fix_admin_menu_submenu', 11);
function fix_admin_menu_submenu() {
// Add "Example CPT" Custom-Post-Type as submenu of the "Example Parent Page" page
add_submenu_page('example_parent_page_id', 'Example CPT', 'Example CPT', 'edit_pages' , 'edit.php?post_type=example_cpt');
}
Please note the priority 11, and also when registering the Custom-Post-Type I set the "show_in_menu" parameter to false, so we can add it in the menu manually via add_submenu_page as shown above.
Properly set the Custom-Post-Type submenu entry as "active"
Now, the above solution works fine, however when creating/editing a post of the "example_cpt" Custom-Post-Type, it is not set as active and the submenu is not unfolded. Here is how to make sure that it is set as active, as well as the submenu in which it resides is properly set as active when creating/editing a post of the "example_cpt" Custom-Post-Type:
// Set the "example_parent_page_id" submenu as active/current when creating/editing a "example_cpt" post
add_filter('parent_file', 'fix_admin_parent_file');
function fix_admin_parent_file($parent_file){
global $submenu_file, $current_screen;
// Set correct active/current menu and submenu in the WordPress Admin menu for the "example_cpt" Add-New/Edit/List
if($current_screen->post_type == 'example_cpt') {
$submenu_file = 'edit.php?post_type=example_cpt';
$parent_file = 'example_parent_page_id';
}
return $parent_file;
}
Fine-tuning: Rename the first submenu entry
Furthermore, I also wanted the first menu entry of my submenu to be named differently from the parent name. By default, and using the code above, this is what we have:
- Example Parent Page
-- Example Parent Page
-- Example CPT
So as you can see, the first menu entry of the submenu is a duplicate of the parent menu, and this is the default WordPress behavior. I wanted to rename this duplicate entry to something different, much like WordPress does with the default menus (for example "Posts" and the submenu entry "All Posts" which both point to the same page but are named differently).
Here is how to rename the first submenu entry:
add_action('admin_menu', 'rename_first_submenu_entry', 11);
function rename_first_submenu_entry() {
// Rename first submenu entry (duplicate of parent menu) from "Example Parent Page" to "Submenu Text"
add_submenu_page('example_parent_page_id', 'Example Parent Page', 'Submenu Text', 'edit_pages' , 'example_parent_page_id');
}
Please note the priority 11, so it is renamed after it has been created. And now we have:
- Example Parent Page
-- Submenu Text
-- Example CPT
Please note that "Submenu Text" points to the same location as "Example Parent Page".
You also can simply set 'show_in_menu' in custom post type args to $menu_slug that you set in add_menu_page() that you want to set the CPT as sub menu of and set the priority of admin_menu function to 9 or lower. For example:
First, create a new top-level menu page, with priority set to 9 or lower (it's a must):
add_action( 'admin_menu', 'settings_menu' ), 9 );
function settings_menu() {
add_menu_page( __( 'Page Title' ), 'Menu Title', 'manage_options', 'menu_slug', show_page_callback() );
}
function show_page_callback() {
// show the settings page, plugin homepage, etc.
}
Then create custom post type with 'show_in_menu' arg set to menu_slug that we just set in settings_menu() function.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type('my_custom_post_type',
array(
'labels' => array(
'name' => __('Books', 'mcpt'),
'singular_name' => __('Book', 'mcpt'),
),
'supports' => array('title', 'editor'),
'public' => true,
'show_in_menu' => 'menu_slug',
);
}
Hope it helps.
can't say, what's exactly the reason, but it seems wordpress redirects to the first submenu-item.
So you have to create a new sub-menu-item with the same contents of your parent-menu-item.
add_action('admin_menu', 'my_admin_menu');
function my_admin_menu() {
global $submenu;
add_menu_page('My Menu', 'My Menu', 'administrator', 'my-menu', 'callback_func');
$parent = array('My Menu', 'administrator', 'my-menu', 'My Menu'); // new submenu-itm
$submenu['my-menu'] = fix_menu($submenu['my-menu'], $parent); // adds the new submenu-item at beginning of 'my-menu'-item
}
function fix_menu($submenu) {
array_unshift ($submenu, $parent);
return $submenu;
}
Hope it works for you.