Fieldmanager Wordpress plugin - wordpress

I need to find a way to display values saved in custom meta boxes. Meta Boxes are created with WP Fieldmanager plugin.
Here is the code used to display fields in the post admin:
add_action( 'init', function() {
$fm = new Fieldmanager_Group( array(
'name' => 'sidebar',
'limit' => 0,
'label' => 'Sidebar Section',
'label_macro' => array( 'Section: %s', 'name' ),
'add_more_label' => 'Add another Sidebar Section',
'children' => array(
'name' => new Fieldmanager_Textfield( 'Section Heading' ),
'item' => new Fieldmanager_Textfield( 'Item', array(
'limit' => 0,
'one_label_per_item' => true,
'add_more_label' => 'Add another Item',
) ),
),
)
);
$fm->add_meta_box( 'Sidebar', array( 'post' ) );
} );
You can see this is actually a repeating group which has a repeating field inside. I need to display meta values from those groups.
Thanks.

You have to use foreach() to get out the content:
$repeating_fields = get_post_meta( get_the_ID(), 'sidebar', true); //name of your custom field;
foreach ($repeating_fields as $repeating_field) {
print_r($repeating_field[name]); //use here the name given to your children in array to select the correct one;
}
From here you if know how to arrange a foreach loop you should be good.
If you want to get the metabox content as an array you can do like this:
$repeating_fields = get_post_meta( get_the_ID(), 'sidebar', true);
$name = array(); //define the variable as an array from the start;
foreach ($repeating_fields as $repeating_field) { //start the loop
$name[] = $repeating_field[name]; //get the array content from the meta box
}
echo $name; //echo the array or parts of it

Related

Merge custom fields in checkout page with shipping fields

I'm trying to move custom fields that I created on my checkout page in the Shipping block.
What I've tried is this code: thise are the new fields
// Add a new checkout field
function filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 1:' )
),
'another_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 2:' )
),
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );
// display the extra field on the checkout form
function extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Title' ); ?></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'extra_checkout_fields' );
And this is how I'm trying to move them (replace current fields)
add_filter( 'woocommerce_checkout_fields', 'another_group' );
function another_group( $checkout_fields ){
// 1. We assign a field array to another group here
$checkout_fields['some_field'] = $checkout_fields['shipping']['shipping_first_name'];
$checkout_fields['another_field'] = $checkout_fields['shipping']['shipping_last_name'];
// 2. Remove a field from a previous location
unset( $checkout_fields['shipping']['shipping_first_name'] );
unset( $checkout_fields['shipping']['shipping_last_name'] );
return $checkout_fields;
}
What is happening is that the ['shipping_first_name'] and ['shipping_last_name'] are removed (unset) but nothing appeared on their place.
Is this possible to happen at all?
you can set priority of custom field with your old field priority like below
$fields['shipping']['some_field']['priority'] = 10;
$fields['shipping']['another_field']['priority'] = 20;
Just unset the fields that you don't need as you do already but don't try to override them in this function
add_filter( 'woocommerce_checkout_fields', 'another_group' );
function another_group( $checkout_fields ){
// 2. Remove a field from a previous location
unset( $checkout_fields['shipping']['shipping_first_name'] );
unset( $checkout_fields['shipping']['shipping_last_name'] );
// ... more fields for unset here
return $checkout_fields;
}
Then in your function where you have [extra_fields] change it to [shipping] this will place the custom fields in the shipping block.
// Add a new checkout field
function filter_checkout_fields($fields){
$fields['shipping'] = array(
'some_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 1:' )
),
... so on

Created woocommerce product attribute programmatically and added terms to them but terms are not showing in Product->Attributes

I have created woo-commerce product attributes programmatically by the following code:
$data = array(
'name' => 'My attribute',
'slug' => wc_sanitize_taxonomy_name(wp_unslash('My attribute')),
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 1
);
wc_create_attribute( $data );
and this code adds attributes successfully and are visible to the list of woo-commerce in Products->Attributes, but after that, I tried adding some terms to them which are not affected to the attribute list by using following code:
wp_insert_term( 'term_1' ,'pa_'.$data['slug']);
also, I confirm that when I dump the result of var_dump(get_term_by('name','term_1','pa_'.$data['slug'])) I get result with term id,name,slug,term_taxonomy_id etc., but the problem it these terms are not visible on the woocommerce product attributes and so does on product editing page.
Because I've searched myself a lot before reaching out to a working solution that works for Woocommerce 3.8+, I give this answer in order others to get help on a really confusing topic such as Woocommerce Attributes.
I've created two methods/functions: createAttribute and createTerm based on the sample code that Woocomerce provides as an example: See code
function createAttribute(string $attributeName, string $attributeSlug): ?\stdClass {
delete_transient('wc_attribute_taxonomies');
\WC_Cache_Helper::incr_cache_prefix('woocommerce-attributes');
$attributeLabels = wp_list_pluck(wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name');
$attributeWCName = array_search($attributeSlug, $attributeLabels, TRUE);
if (! $attributeWCName) {
$attributeWCName = wc_sanitize_taxonomy_name($attributeSlug);
}
$attributeId = wc_attribute_taxonomy_id_by_name($attributeWCName);
if (! $attributeId) {
$taxonomyName = wc_attribute_taxonomy_name($attributeWCName);
unregister_taxonomy($taxonomyName);
$attributeId = wc_create_attribute(array(
'name' => $attributeName,
'slug' => $attributeSlug,
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 0,
));
register_taxonomy($taxonomyName, apply_filters('woocommerce_taxonomy_objects_' . $taxonomyName, array(
'product'
)), apply_filters('woocommerce_taxonomy_args_' . $taxonomyName, array(
'labels' => array(
'name' => $attributeSlug,
),
'hierarchical' => FALSE,
'show_ui' => FALSE,
'query_var' => TRUE,
'rewrite' => FALSE,
)));
}
return wc_get_attribute($attributeId);
}
function createTerm(string $termName, string $termSlug, string $taxonomy, int $order = 0): ?\WP_Term {
$taxonomy = wc_attribute_taxonomy_name($taxonomy);
if (! $term = get_term_by('slug', $termSlug, $taxonomy)) {
$term = wp_insert_term($termName, $taxonomy, array(
'slug' => $termSlug,
));
$term = get_term_by('id', $term['term_id'], $taxonomy);
if ($term) {
update_term_meta($term->term_id, 'order', $order);
}
}
return $term;
}
So they can be used like:
createAttribute('Colors', 'my-colors');
createTerm('Red', 'my-red', 'my-colors', 10);
createTerm('Green', 'my-green', 'my-colors', 20);
createTerm('Blue', 'my-blue', 'my-colors', 30);

Setting a default option using CMB2 select field type

I am using CMB2's select to pull in a list of posts that a user can choose from in a custom meta box.
I have added a "blank" option to the options array, but I can't figure out how to make that the default option (ie. <option selected="selected" value="">I'm blank</option>).
I need to do this so I can use an if statement that says if the field is blank, don't show the output box on the site. Right now, even if the user hasn't specifically chosen an option, an option with a value is passed through.
Here's the meta box code:
$link_post_types = array('charter', 'page');
$meta_boxes['ms_metabox'] = array(
'id' => 'ms_metabox',
'title' => __( 'Page Links', 'cmb2' ),
'object_types' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Page Link', 'cmb2' ),
'desc' => __( 'Choose the page this will link to', 'cmb2' ),
'id' => $prefix . 'page_link',
'type' => 'select',
'options' => ms_get_posttype_options($link_post_types),
),
),
);
function ms_get_posttype_options($argument) {
$get_post_args = array(
'post_type' => $argument,
'posts_per_page' => -1,
'orderby' => 'type',
'order' => ASC
);
$options = array();
foreach ( get_posts( $get_post_args ) as $post ) {
$post_type = get_post_type( $post->ID);
$title = get_the_title( $post->ID );
$permalink = get_permalink( $post->ID);
$options[] = array(
'name' => $title . ' : ' . $post_type,
'value' => $permalink,
);
}
$empty_option[] = array(
'name' => 'Please select an option',
'value' => '',
);
$options = array_merge($empty_option, $options);
return $options;
}
There is a default argument but when I tried to apply it as in the example, it didn't work.
Thanks for any help!
I halfway figured it out. The posts I was having problems with were old ones where I had already been messing with the values before I added the blank option - when I created new posts, the default option was the blank one (since it was the first array in the merge).
If anyone has a more foolproof solution I'd like to hear it though!
You can add the following to the meta box fields array:
show_option_none' => true

How can I get the Posts created with ACF in Wordpress?

I added this to my function.php for saving the post:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => 'A title, maybe a $_POST variable' ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
And on the page.php I've got this code:
$postid = get_the_ID();
if($postid ==50){ //50 is a Page I created for the Form
$options = array(
'post_id' => 'new',//$post->ID, // post id to get field groups from and save data to
'field_groups' => array(46), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $options );
}
Now, I think the saving was successfull, how do I get the whole Posts that have been created with this Form?
I tried this (on page with ID 51), but I get nothing:
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'location',
'meta_value' => 'melbourne'
));
if($posts)
{
foreach($posts as $post)
{
the_field('titel');
}
}
The ACF Plugin for Wordpress is very well documented, but I couldnt solve my problem.
http://www.advancedcustomfields.com/resources
according to the ACF form field documentation https://www.advancedcustomfields.com/resources/acf_form/
the "return" field has 2 dynamic placeholders
"(String) The URL to be redirected to after the form is submitted.
Defaults to the current URL with a GET parameter ‘?updated=true’. A
special placeholder ‘%post_url%’ will be converted to post’s
permalink. A special placeholder ‘%post_id%’ will be converted to
post’s ID."
if you add 'return' => '?newpost=%post_id%', for example, you get back your original page with the new post id as URL parameter that you can handle with PHP in your page template
The documentation says you've to set the post_id value inside your $options array to new_post instead of new if you want to create a new post. Also you've to use the new_post array key with an array with data for the new post as value for the key.
Check out the acf_form documentation. This is what I basically mean:
$options = array(
'post_id' = 'new_post',
'new_post' = array(
//new post data
)
);

How to get the values of Wordpress customize checkboxes

I can't figure out how to get the value - whether they are checked or not - from checkboxes in the WP customize manager.
This is the code in functions.php:
$wp_customize->add_setting('social_facebook', array(
'type' => 'option',
));
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook',
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social-icons',
'settings' => 'social_facebook',
'type' => 'checkbox',
)
)
);
And this is how I try to get the value:
<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
.facebook {display:inline!important;}
</style>
<?php }
?>
They values of the checkboxes are either "" (empty) or "1", so the system registers the checking of them. However, I don't know how to get the value through the get_theme_mod approach. Also, they don't have any name values, so I can't get the value through the usual way either.
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
This is an example in my case - if that option is checked, it will echo a "sticky" class in my template.
if the setting 'my_theme_settings[social_facebook]' checkbox is unchecked:
<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>
//if setting is unchecked content here will be shown
<?php } // end if ?>
For full article see: http://themefoundation.com/wordpress-theme-customizer
Try use and customize this (tested, in functions.php:
function mytheme_customize_register( $wp_customize ){
$wp_customize->add_section(
// ID
'layout_section',
// Arguments array
array(
'title' => __( 'Layout', 'my_theme' ),
'capability' => 'edit_theme_options',
'description' => __( 'social needs ;)', 'my_theme' )
)
);
$wp_customize->add_setting(
// ID
'my_theme_settings[social_facebook]',
// Arguments array
array('type' => 'option')
);
$wp_customize->add_control(
// ID
'layout_control',
array(
'type' => 'checkbox',
'label' => __( 'Facebook', 'my_theme' ),
'section' => 'layout_section',
// This last one must match setting ID from above
'settings' => 'my_theme_settings[social_facebook]'
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
to read in template
$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
the problem is in the WP_Customize_Setting::value() it expect to return false
to uncheck the checkbox (or leave the checkbox unchecked) while some program will return '0' or ''.
In my case I have to extend the WP_Customize_Setting and override the value() method to force returning boolean.
<?php
class ForceBooleanSettings
extends WP_Customize_Setting {
public function value() {
return (bool) parent::value();
}
}
// Example on using this extend class
$customizer->add_setting(new ForceBooleanSettings(
$customizer,
'myuniquekey',
array(
'default' => false,
'transport' => 'refresh',
)));
?>
Here is my working solution:
function theme_name_custom_settings( $wp_customize ) {
$wp_customize->add_setting( 'social_facebook', array(
'default' => 1, // Set default value
'sanitize_callback' => 'esc_attr', // Sanitize input
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook', // Setting ID
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social_icons', // No hyphen
'settings' => 'social_facebook', // Setting ID
'type' => 'checkbox',
)
)
);
}
add_action( 'customize_register', 'theme_name_custom_settings' );
Then check it's value (0 or 1) like so:
if ( !get_theme_mod( 'social_facebook' ) ) { // false
return;
}
// or
if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
return;
}
<div class="facebook" <?php echo ( get_theme_mod( 'social_facebook' ) ) ? "style='display:none;'" : "" ?>>

Resources