WordPress 3.2.1
I'm creating separate page templates, however, there are some page templates that should have default custom fields. That is, if someone created a new page of template type "Contact Us", it should by default have custom fields:
Success Message
Email To
Phone Number
etc.
Right now, the only way I can think of knocking this out is by having the admin add those custom fields to the page, and then fill them in. However, this isn't the best method for basically giving an admin a "turn-key" type of feature, i.e., they create a page with a specific template and just fill in the fields.
Thanks guys!
The following will add 1 extra meta box to your (insert-edit) page.
In it you can add custom fields to save.
It does not switch depending on the selected template.
I would recomend using javascript to show/hide fields to do this. Based on ID of the <p>
If you get the meta box working with ALL possible fields you can show I'm willing to help with the javascript to hide and show based on the template.
GL
<?php
add_action( 'add_meta_boxes', 'carrousel_build' );
function carrousel_build()
{
add_meta_box('carrousel', 'Carrousel opties', 'carrousel_options', 'page',/*show on 'pages'*/ 'normal', 'high');
}
//this will add a meta box with custom fields
function carrousel_options ($post)
{ ?>
<p>
Description
</p>
<p id="field1_container">
<label for="field1">Custom field 1</label>
<br/>
<input type="text" id="field1" name="field1" value="<?php echo get_post_meta($post->ID, 'field1', true) ?>" size="25" />
</p>
<p id="field2_container">
<label for="field2">Custom field 2</label>
<br/>
<input type="text" id="field2" name="field2" value="<?php echo get_post_meta($post->ID, 'field2', true) ?>" size="25" />
</p>
<?php }
//save the values of the meta box
add_action( 'save_post', 'post_save' );
function post_save($post_id)
{
// Check permissions
if (isset ($_POST['post_type']) && 'carrousel' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ))
{
return;
}
if (isset($_POST['field1'])) {
$subtitle = $_POST['field1'];
update_post_meta($post_id, 'field1', $subtitle);
}
if (isset($_POST['field2']))
{
$link = $_POST['field2'];
update_post_meta($post_id, 'field2', $link);
}
}
Related
The problem is i need a custom field for my buyers to input some dates in a textarea, and when i make the code for it with a plugin or hardcode a function for it, the code makes 2 input fields as shown in this picture/gyazo, i i cant seem to figure out why :/
https://gyazo.com/97e61de41071324491268258d84f8aae
You can see the code in the next gyazo:
https://gyazo.com/e811efd8b0e9c60b361dc52027693797
I have tried everything that i could figure out with my compatebilities
//Custom Product Option
function custom_field_by_category() {
global $product;
if ( is_product_category( 'work-date' ) ) {
return;
}
?>
<div class="custom-date-field">
<label for="custom-date"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="custom-date" name="dato_oensker" placeholder="<?php _e( 'Dato Ønsker', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_field_by_category', 10 );
I know there will be this type of questions but i have tried almost all options without luck so i'm asking here.
I have issue that cart is not empty in woocommerce after order completed and also some users just add items to cart then not go to checkout and after somedays come back again they still see old cart.
I think it may be caching issue as i'm using W3-total-cache plugin.
I want to clear cart data after checkout or if user add items to cart and not checkout and after a while come back again for shopping then cart must be empty and should not store old session to cart.
I tried below code but no luck :
function nom_empty_cart_init_login(){
$opt = get_option('nom_empty_cart_browser_close_do_login');
$opt = $opt != 1 ? false : true;
if( $opt ){
session_destroy();
}
}
// destroy session on user logout
function nom_empty_cart_init_logout(){
$opt = get_option('nom_empty_cart_browser_close_do_logout');
$opt = $opt != 1 ? false : true;
if( $opt ){
session_destroy();
}
}
add_action('admin_menu','nom_empty_cart_init_admin_init');
function nom_empty_cart_init_admin_init(){
add_options_page( 'Woocommerce Clear Cart on Browser Closing', 'WC Clear Cart on Browser Close', 'manage_options', 'wc-clear-cart-on-browser-close', 'wc_clear_cart_on_browser_close' );
}
function wc_clear_cart_on_browser_close(){
if( isset( $_REQUEST['save_accconc'] ) and wp_verify_nonce($_REQUEST['wc-clear-cart-on-browser-close-name'],'wc-clear-cart-on-browser-close-action')):
// SAVING THE FORM DATA
// enable wcccobc
if( isset($_REQUEST['enable_wcccobc']) )
update_option('nom_empty_cart_browser_close_enable',1);
// enable wcccobc on login
if( isset($_REQUEST['enable_wcccobc_on_login']) )
update_option('nom_empty_cart_browser_close_do_login',1);
// enable wcccobc on logout
if( isset($_REQUEST['enable_wcccobc_on_logout']) )
update_option('nom_empty_cart_browser_close_do_logout',1);
// SAVING ;) ENDS
endif;
?>
<div class="wrap">
<div class="inside">
<h2>Woocommerce Clear Cart on Browser Closing</h2>
<p>Note: the cart will be empty if the visitor close the whole browser, not just the widow. (will be updated soon)</p>
<form action="<?php admin_url('options-general.php?page=wc-clear-cart-on-browser-close');?>" method="post">
<?php wp_nonce_field('wc-clear-cart-on-browser-close-action','wc-clear-cart-on-browser-close-name')?>
<p>
<input id="enable_wcccobc" type="checkbox" class="checkbox" name="enable_wcccobc" value="1" <?php checked(get_option('nom_empty_cart_browser_close_enable'),'1');?>>
<label for="enable_wcccobc" >Enable clear cart on browser closing</label>
</p>
<p>
<input id="enable_wcccobc_on_login" type="checkbox" class="checkbox" name="enable_wcccobc_on_login" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_login'),1);?>>
<label for="enable_wcccobc_on_login" >Enable clear cart on on user login</label>
</p>
<p>
<input id="enable_wcccobc_on_logout" type="checkbox" class="checkbox" name="enable_wcccobc_on_logout" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_logout'),1);?>>
<label for="enable_wcccobc_on_logout">Enable clear cart on user logout</label>
</p>
<p>
<input type="submit" class="button-primary" value="Save" name="save_accconc">
</p>
</form>
</div>
</div>
<?php
}
I'm using method from #shoelaced and it work.
edit on theme's functions.php
add_action( 'woocommerce_payment_complete', 'order_received_empty_cart_action', 10, 1 );
function order_received_empty_cart_action( $order_id ){
WC()->cart->empty_cart();
}
I would like to add a meta box in the manual admin orders page of woocommerce.
This meta box will need to be a checkbox that if selected triggers an action
so far from what i have read it will need to start something like this
function add_meta_boxes()
{
add_meta_box(
'Meta Box', // ID, should be a string.
'woocommerce-action-trigger', // Meta Box Title.
'my_meta_box', // Your call back function, this is where your form field will go.
'shop_order', // The post type you want this to show up on, can be post, page, or custom post type.
'side', // The placement of your meta box, can be normal or side.
'default' // The priority in which this will be displayed.
);
}
Then i need a form that if admin selects will trigger an action
function triggeraction_meta_box() {
$checkboxMeta = make_action_happen();
}
<input type="checkbox" name="action" id="action" value="yes" <?php if ( isset ( $checkboxMeta['action'] ) ) checked( $checkboxMeta['action'][0], 'yes' ); ?> />make_action_happen<br />
As you can see at this point i want the checkbox to unhook something in woocommerce for this order only, so it only happens in this page for this session.
Anybody please help my coding is very very very basic.
function wporg_add_custom_box() {
$screens = ['shop_order', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_wporg_meta_key', true);
?>
<label for="wporg_field">Description for this field</label>
<select name="wporg_field" id="wporg_field" class="postbox">
<option value="">Select something...</option>
<option value="something" <?php selected($value, 'something'); ?>>Something</option>
<option value="else" <?php selected($value, 'else'); ?>>Else</option>
</select>
<?php
}
function wporg_save_postdata($post_id) {
if (array_key_exists('wporg_field', $_POST)) {
update_post_meta(
$post_id,
'_wporg_meta_key',
$_POST['wporg_field']
);
}
}
add_action('save_post', 'wporg_save_postdata');
I am creating a plugin I want a functionality of the text fields in admin panel options of the plugin in which I want to set the API key and URL and other things. So how can I add the three text fields in custom plugin?
First of all you should create a admin menu page for the plugin options.
use the below code in your plugin page.
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu', 'my_menu_output' );
}
function my-menu(){
// code for displaying text areas for api key and url
$html = 'Enter url : <input type="text" name="url" />';
$html.= 'Enter Api key : <input type="text" name="api" />';
$html.= '<input type="submit" value="save" name="save" />';
echo $html;
}
Unable to add custom fields in custom taxonomy meta box in wordpress-3.5.2.
I have checked solution in various blogs but Unable to solved this problem. I am using wordpress-3.5.2
What I am trying is :-
// A callback function to add a custom field to our "adtag" taxonomy
add_action( 'adtag_edit_form_fields', 'adtag_callback_function', 10, 2);
// A callback function to save our extra taxonomy field(s)
add_action( 'edited_adtag', 'save_taxonomy_custom_fields', 10, 2 );
I have tried solution from below link:-
http://www.codehooligans.com/2010/07/07/custom-meta-for-new-taxonomies-in-wordpress-3-0/
http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies
http://www.wpbeginner.com/wp-tutorials/how-to-add-additional-custom-meta-fields-to-custom-taxonomies/
http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
Take a look at the Tax-meta-class developed to add extra fields to taxonomies: WordPress Taxonomies Extra Fields the easy way
1) Include the main class file
require_once("Tax-meta-class/Tax-meta-class.php");
2) Configure taxonomy custom fields
$config = array(
'id' => 'demo_meta_box',
'title' => 'Demo Meta Box',
'pages' => array('category'),
'context' => 'normal',
'fields' => array(),
'local_images' => false,
'use_with_theme' => false
);
3) Initiate your taxonomy custom fields
$my_meta = new Tax_Meta_Class($config);
4) Add fields
//text field
$my_meta->addText('text_field_id',array('name'=> 'My Text '));
//textarea field
$my_meta->addTextarea('textarea_field_id',array('name'=> 'My Textarea '));
5) Finish Taxonomy Extra fields Deceleration [important!]
$my_meta->Finish();
6) Getting Saved data
$saved_data = get_tax_meta($term_id,'text_field_id');
echo $saved_data;
To add a custom field to your custom taxonomy, add the following code to your theme's functions.php:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
Next, we'll create a callback function that we'll use to save our custom fields. Add the following code to your theme's functions.php:
// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ){
if ( isset( $_POST['term_meta'][$key] ) ){
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}
The code above will work "as is" for one or more custom taxonomies, no changes needed.
now let's associate these callback functions to the "edit" screen for our custom taxonomies. To do that, we'll use two of the WordPress action hooks that are available for each custom taxonomy that we create. Add the following code to your theme's functions.php:
// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
// Save the changes made on the "presenters" taxonomy, using our callback function
add_action( 'edited_presenters', 'save_taxonomy_custom_fields', 10, 2 );
To access a custom field added to your custom taxonomy
add the following code inside your custom taxonomy template (example, taxonomy-presenters.php), within the PHP block at the top:
// Get the custom fields based on the $presenter term ID
$presenter_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );
// Return the value for the "presenter_id" custom field
$presenter_data = get_userdata( $presenter_custom_fields[presenter_id] ); // Get their data
For this example to work, be sure that you have saved a value in the custom field for the term you are working with.
<?php
echo '<pre>';
print_r( $presenter_custom_fields );
echo '</pre>';
?>
I was able to follow the directions on http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies to create custom fields within a custom taxonomy.
It looks like you are not including the steps after adding the action. Make sure you are working in the functions.php file and that you include the html markup for how the custom field should appear. That is, this section from the SabraMedia instructions:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
The creation / edition of an existing taxonomy, whether customized or not, has two panels.
The first panel to create a new term for the taxonomy normally consisting of meta fields
Name
Slug
And the second panel to edit an existing term
To add custom fields to the taxonomy term creation panel, first panel, use:
<?php
// Key of your custom taxonomy goes here.
// Taxonomy key, must not exceed 32 characters.
$prefix_taxonomy = 'category';
/**
* This will add the custom meta field to the add new term page.
*
* #return void
*/
function wporg_prefix_add_meta_fields(){
?>
<div class="form-field term-meta-wrap">
<label for="term_meta[custom_term_meta]">
<?php esc_html_e( 'Example meta field', 'textdomain' ); ?>
</label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="" />
<p class="description">
<?php esc_html_e( 'Enter a value for this field.', 'textdomain' ); ?>
</p>
</div>
<?php
}
add_action( sprintf( '%s_add_form_fields', $prefix_taxonomy ), 'wporg_prefix_add_meta_fields' );
https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/
To add custom fields to the edit panel, second panel, use:
<?php
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
https://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies