How validate input and output of embed video field - wordpress

I use CMB2 — WordPress Plugins to create Custom Metaboxes. I have a textarea for embeding video. I need to validate the field in right way both on input and output.
$cmb = new_cmb2_box( array(
'id' => $prefix . 'video_box',
'title' => __( 'Video - Embeded from YouTube or Vimeo', 'ttt' ),
'object_types' => array( 'post' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true
) );
$cmb->add_field( array(
'name' => __( 'Embed Code', 'ttt' ),
'id' => $prefix . 'embed_code',
'type' => 'textarea',
'sanitization_cb' => false,
) );
Here is how I use it the field.
$embed_code = get_post_meta($id, 'ttt_embed_code', true )

Related

Meta box tabs - in a custom post type

Is it possible to insert meta box tabs in a specific custom post type.
I've been looking at the example :
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
// 1st Meta Box
$meta_boxes[] = array(
'title' => 'Meta Box Tabs Demo',
// List of tabs, in one of the following formats:
// 1) key => label
// 2) key => array( 'label' => Tab label, 'icon' => Tab icon )
'tabs' => array(
'contact' => array(
'label' => 'Contact',
'icon' => 'dashicons-email', // Dashicon
),
'social' => array(
'label' => 'Social Media',
'icon' => 'dashicons-share', // Dashicon
),
'note' => array(
'label' => 'Note',
'icon' => 'https://i.imgur.com/nJtag1q.png', // Custom icon, using image
),
),
// Tab style: 'default', 'box' or 'left'. Optional
'tab_style' => 'default',
// Show meta box wrapper around tabs? true (default) or false. Optional
'tab_wrapper' => true,
'fields' => array(
array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
// Which tab this field belongs to? Put tab key here
'tab' => 'contact',
),
array(
'name' => 'Email',
'id' => 'email',
'type' => 'email',
'tab' => 'contact',
),
array(
'name' => 'Facebook',
'id' => 'facebook',
'type' => 'text',
'tab' => 'social',
),
array(
'name' => 'Note',
'id' => 'note',
'type' => 'textarea',
'tab' => 'note',
),
),
);
This will work in post if added to functions.php ... but if we try to use it in a custom post type the metabox will appear but the fields will not.
Any help would be appreciated.

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' );```

How to add a custom field in woocommerce product category using meta box plugin

I just want to add a custom field in the product category page in admin using the metabox plugin https://metabox.io/
This is in woocommerce and this is what I have
add_filter( 'rwmb_meta_boxes', 'product_register_meta_boxes' );
function product_register_meta_boxes( $meta_boxes )
{
$prefix = 'product_';
$meta_boxes[] = array(
'id' => 'product_cat_keywords_search',
'title' => __( 'Searchable keywords', 'your-prefix' ),
'post_types' => array( 'product'),
'taxonomy' => array( 'product_cat'),
'context' => 'normal',
'priority' => 'low',
'fields' => array(
array(
'name' => __( 'Product Category Keywords', 'your-prefix' ),
'id' => "{$prefix}pcat_keywords",
'desc' => __( 'Category Keywords', 'your-prefix' ),
'type' => 'text',
'std' => __( '', 'your-prefix' ),
'clone' => true,
'clone-group' => 'my-clone-group2',
),
)
);
return $meta_boxes;
}
This code only appears in product page and not in product category page.
I need to use metabox since I will be using the clone feature, thanks

WordPress admin: How can I display existing admin sub-menu based on user's role

I require to show an existing sub-menu item of admin menu based on a specific user role. The role is general-travel-editor.
The actual menu layout is like this
Travel Settings
- General settings
- Advanced Settings
The above user role is supposed access only the "General Settings" sub-menu. So I want to display the menu for this type users as:
Travel Settings
- General settings
I tried add_menu_page( 'Travel Settings', 'Travel Settings', 'general-travel-editor', 'general-travel-editor'); but I don't have the option to add the URL of this page.
I am using a VIP plugin.
I need to deliver this by tonight but got stuck!
Somebody please advise how I can do this.
UPDATE
User role and capabilities defined in functions.php
function general_travel_user_capabilities() {
$travel_option_caps = array(
'publish_travel-options' => true,
'edit_travel-options' => true,
'edit_others_travel-options' => true,
'edit_travel-option' => true,
'read_travel-option' => true,
'delete_travel-options' => true,
'delete_travel-option' => true,
'read' => true
);
wpcom_vip_duplicate_role('subscriber', 'travel-option-editor', 'Travel Option Editor', $travel_option_caps);
$travel_option_caps = array(
'publish_travel-options',
'edit_travel-options',
'edit_others_travel-options',
'delete_travel-options',
'delete_others_travel-options',
'read_private_travel-options',
'edit_travel-option',
'delete_travel-option',
'read_travel-option'
);
wpcom_vip_add_role_caps('editor', $travel_option_caps);
wpcom_vip_add_role_caps('administrator', $travel_option_caps);
}
add_action('init', 'general_travel_user_capabilities');
Travel Settings:
function travel_setting_pages_init() {
$travel_settings = travel_create_settings_page(
'travel_settings_page',
'Global Travel Settings',
array(
'parent' => false,
'title' => 'Travel Settings',
'position' => '59.4815152342',
),
array(
'travel_theme_options' => array(
'title' => '',
'description' => 'Settings that affect all pages of the site',
'fields' => array(
'mytravel_root_url' => array(
'type' => 'url',
'label' => 'MyTravel Root URL',
'description' => 'The base url for all MyTravel App links',
'default' => 'http://myurl.com',
),
'travel_phone' => array(
'type' => 'text',
'label' => 'Support Phone Number U.S.',
'description' => 'In top left of all pages',
'default' => '800-610-6500',
),
'travel_phone_canada' => array(
'type' => 'text',
'label' => 'Support Phone Number Canada',
'description' => 'In top left of all pages',
'default' => '877-330-3321',
),
'travel_weather_text' => array(
'type' => 'text',
'label' => 'Alert Text',
'description' => 'Text for the alert on the home page. Required for the alert to be displayed.',
'default' => '',
),
'travel_weather_link' => array(
'type' => 'url',
'label' => 'Alert Link',
'description' => 'Link for the alert on the home page. Required for the alert to be displayed.',
'default' => '',
),
'travel_weather_start_date' => array(
'type' => 'date',
'label' => 'Alert Start Date',
'description' => 'Start date to display the alert on the home page. Optional.',
'default' => '',
),
'travel_weather_end_date' => array(
'type' => 'date',
'label' => 'Alert end Date',
'description' => 'End date to display the alert on the home page. Optional.',
'default' => '',
)
)
)
)
);
// Home Page Settings Sub-Page
$slide_color_options = array(
'orange-grunge' => 'Orange Grunge Pattern',
'orange-img1' => 'Orange Image: Truck Engine with Person',
'orange-img2' => 'Orange Image: Truck Top with Person',
'orange-img3' => 'Orange Image: Forklift',
'orange-trailers' => 'Orange Image: Truck Trailers',
'black-trails' => 'Black Image: Light Trails',
'dark-grey' => 'Grey Image: Hand',
'white' => 'White Image: Trucks in Dock',
'orange' => 'Orange (To be removed)',
'blue' => 'Blue (To be removed)',
'black' => 'Black (To be removed)'
);
$travel_home_settings = travel_create_settings_page(
'travel_home_settings_page',
'Home Page Settings',
array(
'parent' => 'travel_settings_page',
'title' => 'Home Page Settings',
),
array(
'travel_home_slideshow_settings' => array(
'title' => 'Home Slideshow Settings',
'description' => 'If image is not set, slide will not be shown.',
'fields' => array(
// Slide 1
'slide_color_1' => array(
'type' => 'radio',
'label' => 'Slide 1 Background Color',
'default' => 'orange',
'options' => $slide_color_options
),
'slide_image_1' => array(
'type' => 'media',
'label' => 'Slide 1 Image',
'description' => 'Transparent PNG # 911x375. See travel-home-marquee-template.psd',
),
'slide_heading_1' => array(
'type' => 'textarea',
'label' => 'Slide 1 Heading',
),
'slide_sub_heading_1' => array(
'type' => 'wysiwyg',
'label' => 'Slide 1 Sub-Heading',
),
)
),
'travel_home_mid_cta_settings' => array(
'title' => 'Middle CTA Settings',
'fields' => array(
'mid_cta_heading_1_1' => array(
'type' => 'text',
'label' => 'Middle CTA 1 - First Line',
),
)
),
'travel_home_lower_cta_settings' => array(
// first block
'title' => 'Lower CTA Settings',
'fields' => array(
'lower_cta_image_1' => array(
'type' => 'media',
'label' => 'Lower CTA 1 - Header Image',
'description' => 'Transparent PNG # 193x71. If set, overrides main heading text.',
),
)
)
)
);
}
add_action( 'init', 'travel_setting_pages_init' );
travel-option is a Custom Post Type so I could use it in caps array. When logging in with the said user role I can see the CPT menus are coming up nicely but how can I add this settings menu? I apologize for posting such a long code!
I will leave another answer here for anyone facing the same situation.
For me, I was creating a plugin and wanted to give it a role different than (Administrator)
I searched for the already existing wp roles and chose "Author" role. Roles are mentioned in this link
Here's how I added my menu =>
add_menu_page( 'Page Title', 'Menu title', 'upload_files', 'url slug', 'function name to be executed', $logo , 1 );
So you just have to replace the 'upload_files' with any other capability matching the selected role.
The add_submenu_page function - which you should use for "advanced settings" is defined like this:
add_submenu_page( string $parent_slug, string $page_title, string
$menu_title, string $capability, string $menu_slug, callable $function
= '' );
As you can see you can define a certain $capability for displaying your menu or submenu page.
You can either use a capability which is only available to admins like manage_options or you could even define a special capability like this:
$role = get_role( 'administrator' );
$role->add_cap( 'edit_advanced_travel_settings' );
https://codex.wordpress.org/Function_Reference/add_cap
If you do it correctly the advanced page should only be visible to users whom you have granted this capability.
Note: Within the custom admin page callback - for security reasons - you should also check if the current user has the required capability.
if(current_user_can('edit_advanced_travel_settings')) {
// display page
} else {
echo "Sorry advanced travel settings are not available to you.";
}

make CMB text_datetime_timestamp field repeatable

I'm using CMB to creat custom fields for wordpress custom post
https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
I'm using text_datetime_timestamp to set date and time and I need to make this field repeatable which is not
as I go through the documentation we can make new fields but I can't figure out how it make "text_datetime_timestamp" field repeatable
any body can show me the way to accomplish that ?
thank you
I did not find a solution to make the field repeatable so I put the field inside group and made it repeatable
array(
'id' => $prefix . 'repeat_date_group',
'type' => 'group',
'description' => '',
'options' => array(
'group_title' => __( 'Date/Time {#}', 'cmb' ),
'add_button' => __( 'Add Another Date/Time', 'cmb' ),
'remove_button' => __( 'Remove Date/Time', 'cmb' ),
'sortable' => true, // beta
),
'fields' => array(
array(
'name' => 'Date/Time',
'desc' => '',
'id' => $prefix . 'course_date',
'type' => 'text_datetime_timestamp'
),
),
),
I hope this answer will help somebody
Well here is my code, you may try this
$cmb= new_cmb2_box( array(
'id' => $prefix.'testing',
'title' => _('Testing'),
'object_types' => array('post'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb->add_field( array(
'name' => 'Test Date/Time Picker Combo (UNIX timestamp)',
'id' => 'wiki_test_datetime_timestamp',
'type' => 'text_datetime_timestamp',
'repeatable' => true,
) );

Resources