how to disable display custom field in page template? - wordpress

I created my custom fields through code and then I registered it with this code:
/**
* Content blocks for all pages
*/
$location_page = new ACF_Group_Location;
$location_page->add_rule( 'post_type', 'page');
$location_slots = new ACF_Group_Location;
$location_slots->add_rule( 'post_type', 'slots' );
$location_card = new ACF_Group_Location;
$location_card->add_rule( 'post_type', 'card' );
ACF_Group::create( 'content-blocks', __( 'Content blocks', 'dw' ) )
->add_location( $location_page )
->add_location( $location_slots )
->add_location( $location_card )
->set_attr( 'label_placement', 'top' )
->set_attr( 'hide_on_screen', array( 'the_content', 'comments' ) )
->add_fields( array(
array(
'label' => __( 'Content blocks', 'dw' ),
'name' => 'cb',
'type' => 'flexible_content',
'button_label' => __( 'Add content block', 'dw' ),
'layouts' => $cb
),
)
)->register();
add_rule() includes:
public function add_rule( $param, $value, $operator = '==' ) {
$this->rules[] = compact( 'param', 'value', 'operator' );
return $this;
}
and my question is what code do I need to add if I wanna disable them on my page template?

So you're using ACF programatically using this
instead of adding your custom fields to all pages and then try to disable it for a particular template just add your custom field group to the template you need.
<?php
add_action( 'register_acf_groups', 'my_theme_register_acf_groups' );
function mytheme_register_acf_groups() {
ACF_Group::create( 'my_group', 'My Group' )
->add_location_rule( 'post_type', 'page' )
->add_location_rule( 'page_template', 'template-contact.php' ) // <- see here
->set_attr( 'label_placement', 'left' )
->add_fields(array(
array(
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text'
),
))
->register();
}

Related

Can't add a section to a Woocommerce Custom Setting Tab [duplicate]

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?
1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

How to display it on the product page?

With this function I am getting the option to use a custom selectbox in my products. But it doesn't appear in the product page meta. I would like to display it just for one special product. How can I do that?
I don't have any PHP experience.
<?php
function prefix_add_selectbox() {
$args = array(
'id' => 'my_new_select', // required. The meta_key ID for the stored value
'wrapper_class' => '', // a custom wrapper class if needed
'desc_tip' => true, // makes your description show up with a "?" symbol and as a tooltip
'description' => __('My awesome select box', 'your_text_domain'),
'label' => __( 'My New Select', 'your_text_domain' ),
'options' => array(
'value1' => __( 'Text 1', 'your_text_domain' ),
'value2' => __( 'Text 2', 'your_text_domain' )
)
);
woocommerce_wp_select( $args );
}
add_action( 'woocommerce_product_options_tax', 'prefix_add_selectbox' );```

WordPress WooCommerce API: Update/insert custom taxonomy terms

I am trying to update a products custom taxonomy term via the wp-json api.
I have added the below function to handle taxonomies:
function wp_api_add_tax($post, $data, $update){
foreach( $data['custom_tax'] as $tax => $val ){
wp_set_post_terms( $post['ID'], $val, $tax );
}
}
add_filter('json_insert_post', 'wp_api_add_tax', 10, 3);
And then add then taxonomy term like this:
$api_response = wp_remote_post( 'https://example.com/wp-json/wc/v2/products/4286', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'xxxxxxxxxxxx:xxxxxxxxxx' )
),
'body' => array(
'stock_quantity' => '0',
'categories' => array(
array(
'id' => 373
),
array(
'name' => 'Build Lean Muscle'
)
),
'custom_tax' => array(
'My Taxonomy Here' => 'My Term Here'
),
'meta_data' => array(
array(
'key' => 'nutritional',
'value' => 'nutritional description by the api 2'
)
)
)
) );
Is this the correct way of doing things? The custom taxonomy term does not get updated.

Wordpress: pre_get_posts and order by taxonomy term

I have a category archive with a custom taxonomy "client". Now I want to order the category archive by the term of the custom taxonomy (the client name). I tried to query it by meta_value and a advanced custom taxonomy query, but I could not get it to be ordered by the name of the taxonomy term. Any suggestions to get a Wordpress loop ordered by taxonomy term name? See line $query->set( 'orderby', 'how_to_order_by_taxonomy_name' );
Registering the taxonomy
add_action( 'init', 'jp_register_project_taxonomies', 0 );
function jp_register_project_taxonomies() {
// register taxonomy to hold our clients
register_taxonomy(
'client',
array( 'post' ),
array(
'hierarchical' => false,
'public' => true,
'query_var' => true,
'rewrite' => true,
'labels' => array(
'name' => _x( 'Clients', 'taxonomy general name' ),
'singular_name' => _x( 'Client', 'taxonomy singular name' ),
'search_items' => __( 'Search Clients' ),
'all_items' => __( 'All Clients' ),
'edit_item' => __( 'Edit Client' ),
'update_item' => __( 'Update Client' ),
'add_new_item' => __( 'Add New Client' ),
'new_item_name' => __( 'New Client Name' ),
'menu_name' => __( 'Client' )
),
)
);
}
pre_get_posts Query
add_action( 'pre_get_posts', 'jp_project_taxonomy_queries' );
function jp_project_taxonomy_queries( $query ) {
if(!is_admin() && $query->is_main_query() && is_category() ):
if (get_query_var('poby') == 'client'):
$taxonomies = array();
$tax_order = (get_query_var('po') == 'DESC' ? 'DESC' : 'ASC' );
foreach (get_terms('client', array('order' => $tax_order)) as $tax ) {
$taxonomies[] = $tax->name;
}
$taxquery = array(
array(
'taxonomy' => 'client',
'terms' => $taxonomies,
'field' => 'slug',
)
);
$query->set( 'tax_query', $taxquery );
$query->set( 'orderby', 'how_to_order_by_taxonomy_name' );
endif;
endif;
}
As far as I know, there is no parameter to order a WP_Query by term.
My solution to this problem usually is to create a meta field that contains the term name or the term slug. This field is created when the post is saved using the hook save_post or save_post_{post_type}.
For instance, to order a list of books (post type book) by year of publication (taxonomy date) we can use this function to create the meta field:
add_action( 'save_post_book', 'prefix_save_date_as_meta', 10 );
function prefix_save_date_as_meta ($post_id) {
$years = get_the_terms($post_id,'date');
if( empty($years) )
return;
$years_list = wp_list_pluck($years,'name');
update_post_meta($post_id,'_book_date',$years_list[0]);
return;
}
And this other function to order the query:
add_filter( 'pre_get_posts', 'prefix_custom_args_for_loops' );
function prefix_custom_args_for_loops( $query ) {
if ( !is_admin() && is_post_type_archive('book') && $query->is_main_query() ) {
$query->set( 'orderby','meta_value_num');
$query->set( 'meta_key','_book_date');
}
return $query;
}

Showing custom taxonomy column in custom posts type listings

I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is to be shown. Everything is great however when I click on the custom posts type 'Banner' in the admin window I see all the banners created however the table does not have a column for the taxonomy 'Location'.
In other words I want to be able to see what location the banner is in, in the banners listing.
For those interested, the register_taxonomy register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.
You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}
function my_new_custom_post_column( $column ) {
$column['location'] = 'Location';
return $column;
}
function location_tax_column_info( $column_name, $post_id ) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if (!empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join('', $post_terms );
}
else echo '<i>No Location Set. </i>';
}
//what version of wordpress you are using
//since wp 3.5 you can pass parameter show_admin_column=>true
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
}
In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:
add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);
function filter_post_type_backend_by_taxonomies($post_type, $which) {
if( 'your_post_type' !== $post_type) {
return;
}
$taxonomies = array( 'your_custom_taxonomy' );
foreach( $taxonomies as $taxonomy_slug ) {
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;
$terms = get_terms($taxonomy_slug);
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}

Resources