How to add categori selection option in theme option? - php

I am trying to add a category option in my wordpress theme using option tree. I am using this code in theme-option.php
<?php
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>
But it is not showing any category. Where is my fault? Please tell me.

You need to define $wp_cats variable within your function, or use global to bring it in.
Option 1 (global variable)
<?php
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
global $wp_cats; /** GLOBAL!! */
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>
Option 2 (build variable inside function)
<?php
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>

I have found the solution. Just need to write
'type' => 'category-select',
insted of
'type' => 'select',

Related

Problem with foreach array on select option on woocmmerce function class

I create a function to add different shiping prices foreach user, all works fine for only one user (the first on the list), but i can't get to make the select options foreach user, it only shows 1 option with all the user in it.
the problem is integrate the foreach in here: 'test' => array. It creates the html elemnt but only 1 and not 1 foreach user
my code:
class WC_Shipping_click extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ) {
$args = array(
'role' => '',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ){
$userinfo .= '['.$user->precio.','.$user->first_name.'] ';
}
$this->id = 'click_method';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Click Shipping Method' );
$this->method_description = __( 'Click Shipping' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method' ),
'default' => 'yes',
),
'test' => array(
'title' => __( 'Prueba de array' ),
'description' => $user->first_name,
'type' => 'select',
'options' => array( ''.$userinfo.'' => ''.$userinfo'',),
'label' => __( 'Some field' ),
'required' => true,
)
)
);
$this->enabled = $this->get_option( 'enabled' );
$this->prueba = $this->get_option( 'prueba' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* #param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => $this->test,
) );
}
}
This is the scope of your foreach()
foreach ( $users as $user ){
$userinfo .= '['.$user->precio.','.$user->first_name.'] ';
}
Nothing that is below will fall into this foreach() loop.

Duplicate admin menu settings in wordpress

I am sorry if the title isn't correct, but I have no idea about those coding stuff I don't know how to explain it.
I am using WordPress and my theme has a build-in mega menu.
The problem is that the mega menu settings in admin panel appear twice while inside the file just one.
Example in screenshot below:
Here is the settings file:
<?php if ( ! defined( 'ABSPATH' ) ) { die; }
if(!class_exists('Veera_MegaMenu_Init')){
class Veera_MegaMenu_Init{
protected $fields = array();
protected $default_metakey = '';
public function __construct() {
$query_args = array(
'post_type' => 'la_block',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 20
);
$this->default_metakey = '_mm_meta';
$this->fields = array(
'icon' => array(
'id' => 'icon',
'type' => 'icon',
'title' => esc_html__('Custom Icon','veera')
),
'nolink' => array(
'id' => 'nolink',
'type' => 'switcher',
'title' => esc_html__("Don't link",'veera')
),
'only_icon' => array(
'id' => 'only_icon',
'type' => 'switcher',
'title' => esc_html__("Show Only Icon",'veera')
),
'hide' => array(
'id' => 'hide',
'type' => 'switcher',
'title' => esc_html__("Don't show a link",'veera')
),
'menu_type' => array(
'id' => 'menu_type',
'type' => 'select',
'title' => esc_html__('Menu Type','veera'),
'options' => array(
'narrow' => esc_html__('Narrow','veera'),
'wide' => esc_html__('Wide','veera')
),
'default' => 'narrow'
),
'submenu_position' => array(
'id' => 'submenu_position',
'type' => 'select',
'title' => esc_html__('SubMenu Position','veera'),
'info' => esc_html__('Apply for parent with "Menu Type" is "narrow"','veera'),
'options' => array(
'right' => esc_html__('Right','veera'),
'left' => esc_html__('Left','veera'),
),
'default' => 'right'
),
'force_full_width' => array(
'id' => 'force_full_width',
'type' => 'switcher',
'title' => esc_html__('Force Full Width','veera'),
'info' => esc_html__('Set 100% window width for popup','veera')
),
'popup_max_width' => array(
'id' => 'popup_max_width',
'type' => 'number',
'title' => esc_html__('Popup Max Width','veera'),
'after' => 'px',
'wrap_class' => 'la-element-popup-max-width'
),
'popup_column' => array(
'id' => 'popup_column',
'type' => 'select',
'title' => esc_html__('Popup Columns','veera'),
'options' => array(
'1' => esc_html__('1 Column','veera'),
'2' => esc_html__('2 Columns','veera'),
'3' => esc_html__('3 Columns','veera'),
'4' => esc_html__('4 Columns','veera'),
'5' => esc_html__('5 Columns','veera'),
'6' => esc_html__('6 Columns','veera')
),
'default' => '4'
),
'item_column' => array(
'id' => 'item_column',
'type' => 'text',
'title' => esc_html__('Columns','veera'),
'desc' => esc_html__('will occupy x columns of parent popup columns', 'veera')
),
'block' => array(
'id' => 'block',
'type' => 'select',
'title' => esc_html__('Custom Block Before Menu Item','veera'),
'options' => 'posts',
'query_args' => $query_args,
'default_option' => esc_html__('Select a block','veera')
),
'block2' => array(
'id' => 'block2',
'type' => 'select',
'title' => esc_html__('Custom Block After Menu Item','veera'),
'options' => 'posts',
'query_args' => $query_args,
'default_option' => esc_html__('Select a block','veera')
),
'popup_background' => array(
'id' => 'popup_background',
'type' => 'background',
'title' => esc_html__('Popup Background','veera')
),
'tip_label' => array(
'id' => 'tip_label',
'type' => 'text',
'title' => esc_html__('Tip Label','veera')
),
'tip_color' => array(
'id' => 'tip_color',
'type' => 'color_picker',
'title' => esc_html__('Tip Color','veera')
),
'tip_background_color' => array(
'id' => 'tip_background_color',
'type' => 'color_picker',
'title' => esc_html__('Tip Background','veera')
)
);
$this->load_hooks();
}
private function load_hooks(){
add_action( 'wp_loaded', array( $this, 'load_walker_edit' ), 9);
add_filter( 'wp_setup_nav_menu_item', array( $this, 'setup_nav_menu_item' ));
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'add_megamu_field_to_menu_item' ), 10, 4);
add_action( 'wp_update_nav_menu_item', array( $this, 'update_nav_menu_item' ), 10, 3);
add_filter( 'nav_menu_item_title', array( $this, 'add_icon_to_menu_item' ),10, 4);
add_filter( 'nav_menu_css_class', array( $this, 'nav_menu_css_class' ),10, 4);
}
public function load_walker_edit() {
add_filter( 'wp_edit_nav_menu_walker', array( $this, 'detect_edit_nav_menu_walker' ), 99 );
}
public function detect_edit_nav_menu_walker( $walker ) {
$walker = 'Veera_MegaMenu_Walker_Edit';
return $walker;
}
public function setup_nav_menu_item($menu_item){
$meta_value = Veera()->settings()->get_post_meta($menu_item->ID, '', $this->default_metakey, true);
foreach ( $this->fields as $key => $value ){
$menu_item->$key = isset($meta_value[$key]) ? $meta_value[$key] : '';
}
return $menu_item;
}
public function update_nav_menu_item( $menu_id, $menu_item_db_id, $menu_item_args ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
$key = $this->default_metakey;
if ( ! empty( $_POST[$key][$menu_item_db_id] ) ) {
$value = $_POST[$key][$menu_item_db_id];
}
else {
$value = null;
}
if(!empty($value)){
update_post_meta( $menu_item_db_id, $key, $value );
}
else {
delete_post_meta( $menu_item_db_id, $key );
}
}
public function add_megamu_field_to_menu_item( $id, $item, $depth, $args ) {
if(function_exists('la_fw_add_element')){
?>
<div class="lastudio-megamenu-settings description-wide la-content">
<h3><?php esc_html_e('MegaMenu Settings','veera');?></h3>
<div class="lastudio-megamenu-custom-fields">
<?php
foreach ( $this->fields as $key => $field ) {
$unique = $this->default_metakey . '['.$item->ID.']';
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$elem_id = ( isset( $field['id'] ) ) ? $field['id'] : '';
$field['name'] = $unique. '[' . $elem_id . ']';
$field['id'] = $elem_id . '_' . $item->ID;
$elem_value = isset($item->$key) ? $item->$key : $default;
echo la_fw_add_element( $field, $elem_value, $unique );
}
?>
</div>
</div>
<?php
}
}
public function add_icon_to_menu_item($output, $item, $args, $depth){
if ( !is_a( $args->walker, 'Veera_MegaMenu_Walker' ) && $item->icon){
$icon_class = 'mm-icon ' . $item->icon;
$icon = "<i class=\"".esc_attr($icon_class)."\"></i>";
$output = $icon . $output;
}
return $output;
}
public function nav_menu_css_class( $classes, $item, $args, $depth ){
if(!is_a( $args->walker, 'Veera_MegaMenu_Walker' )){
if ( $item->hide ) {
$classes[] = "mm-item-hide";
}
if ( $item->nolink ) {
$classes[] = "mm-item-nolink";
}
if ( $item->only_icon ) {
$classes[] = "mm-item-onlyicon";
}
}
return $classes;
}
}
}
Can you please help me fix that? (Note: I am really newbie with those stuff, please be detailed)
I am sorry and thanks!

Wordpress customizer (kirki) page list / sortable

I'm trying to put in place a sortable field where the options value are the page titles,
I'm using Kirki plugin,
How can i do that ?
here's is the code
Thanks a lot for your time
Using theme_mods or options?
theme_mods
Code to reproduce the issue (config + field(s))
<?php
$pages = get_pages();
foreach ($pages as $page ) {
$pages_list[] = array ('id' => $page->ID, 'title' => $page->post_title);
};
print_r($pages_list);
Kirki::add_field( 'my_config', array(
'type' => 'sortable',
'settings' => 'my_setting',
'label' => __( 'This is the label', 'my_textdomain' ),
'section' => 'my_section',
'default' => array(
'option3',
'option1',
'option4'
),
'choices' => $pages_list,
'priority' => 10,
) );
?>
got it !
$pages_array = array();
$get_pages = get_pages( 'hide_empty=0' );
foreach ( $get_pages as $page ) {
$pages_array[$page->ID] = esc_attr( $page->post_title );
}
Kirki::add_field( '_s_theme', array(
'type' => 'sortable',
'settings' => 'sort',
'label' => __( 'This is the label', 'my_textdomain' ),
'section' => 'sections',
'default' => array(),
'choices' => $pages_array,
'priority' => 10,
) );

List posts in the wordpress theme customiser

Hey there. I am creating a Wordpress theme and was wondering if anyone knew of a way to list one of my custom post types in the theme customiser. I have a custom post type called "slideshow" that has custom meta boxes etc and is designed just for slideshows. I would like to be able to list these posts in a dropdown inside the customiser. Ideally ending up with them in the array like this...
'the_id' => 'Slideshow post title',
$wp_customize->add_setting(
'slideshow-homepage',
array(
'default' => 'none',
)
);
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array(
'somehow' => 'somehow',
'list' => 'list',
'all' => 'all',
'custom' => 'custom',
'post' => 'post',
'types' => 'types',
'of' => 'of',
'type' => 'type',
'slideshow' => 'slideshow'
),
)
);
Many thanks guys and girls. Lewis
use array_reduce
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
),
)
);
To add an empty field as well:
$posts = array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
);
$none = array('' => 'None');
$choices = $none + $posts;
$wp_customize->add_control('slideshow_control', array(
'label' => __('Choose Slideshow', 'themename'),
'section' => 'slideshow_option',
'settings' => 'slideshow_settings',
'type' => 'select',
'choices' => $choices
));

Update wordpress blogname and blogdescription using Redux

Is it possible to update wordpress blogname and blogdescription via Redux framework.
array(
'id' => 'blogdescription',
'type' => 'text',
'title' => 'Blog Description',
'default' => '',
),
You can use update_option(); function
update_option( 'blogname', 'New Value' );
update_option( 'blogdescription', 'New Value' );
Hooking on Admin
add_action('admin_init', 'update_my_site_blog_info');
function update_my_site_blog_info() {
$old = get_option('blogdescription');
$new = 'New Site Title';
if ( $old !== $new ) {
update_option( 'blogdescription', $new );
}
}
EDIT:
I guess its better this way,
add_filter('redux/options/[your_opt_name]/compiler', 'update_my_site_blog_info');
function update_my_site_blog_info() {
$new = 'New Site Title';
update_option( 'blogdescription', $new );
}
then your field needs to enabled compiler
array(
'id' => 'blogdescription',
'type' => 'text',
'title' => 'Blog Description',
'default' => '',
'compiler' => true,
),
Thanks for the help, i did like this to make it work.
add_action('init', 'update_my_site_blog_info');
function update_my_site_blog_info()
{
global $opt_keyname;
$check = array('blogdescription', 'blogname');
foreach($check as $key)
{
if ( get_option($key) != $opt_keyname[$key] )
{
update_option( $key, $opt_keyname[$key] );
}
}
}
Redux::setSection( $opt_name,
array(
'title' => 'Basic Settings',
'id' => 'basic_settings',
'fields' => array(
array(
'id' => 'blogname',
'type' => 'text',
'title' => 'Blog Title',
'default' => get_option( 'blogname' )
),
array(
'id' => 'blogdescription',
'type' => 'text',
'title' => 'Blog Description',
'default' => get_option( 'blogdescription' )
),
)
)
);

Categories