Update wordpress blogname and blogdescription using Redux - php

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

Related

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!

Custom divi builder child module not rendering

I am trying to create some custom Divi builder modules.
I followed the sparse documentation at their website, trying to create a module and a child module. Everything seems to be working fine, but the rendering.
It is just rendering the module shortcode as a string, and not the content.
Current code of the parent module is
class DEDE_Cards extends ET_Builder_Module {
public function init(){
$this->name = esc_html__('Custom Card', 'dede-designvox-divi-extension');
$this->plural = esc_html__('Custom Cards', 'dede-designvox-divi-extension');
$this->main_css_element = 'cards-wrapper';
$this->slug = 'dede_cards';
$this->vb_support = 'on';
$this->child_slug = 'dede_card_item';
}
public function get_fields(){
return array(
'title' => array(
'label' => esc_html__( 'Title', 'dede-designvox-divi-extension' ),
'type' => 'text',
'toggle_slug' => 'main_content',
'description' => esc_html__( 'Type the section title' ),
),
'card_title_position' => array(
'label' => esc_html__( 'Title Position', 'dede-designvox-divi-extension' ),
'type' => 'select',
'options' => array(
'overlay' => esc_html__( 'Overlay', 'et_builder' ),
'top' => esc_html__( 'Over image', 'et_builder' ),
'bottom' => esc_html__( 'Under image', 'et_builder' ),
),
'toggle_slug' => 'main_content',
'description' => esc_html__( 'Here you can choose the position of the card title', 'dede-designvox-divi-extension' ),
'default_on_front' => 'bottom',
),
);
}
function before_render() {
global $dede_card_item_number;
$dede_card_item_number = 1;
}
public function render($unprocessed_props, $content = null, $render_slug ){
global $dede_card_item_number;
return sprintf(
'<div>%1$s</div>',
$this->content
);
}
public function add_new_child_text() {
return esc_html__( 'Add New Card Item1', 'dede-designvox-divi-extension' );
}
}
new DEDE_Cards;
And then the child module
class DEDE_Card_Item extends ET_Builder_Module {
public function init(){
$this->name = esc_html__('Custom Card', 'dede-designvox-divi-extension');
$this->plural = esc_html__('Custom Cards', 'dede-designvox-divi-extension');
$this->type= 'child';
$this->child_title_var = 'title';
$this->no_render = true;
$this->slug = 'dede_card_item';
$this->vb_support = 'on';
}
public function get_fields(){
return array(
'title' => array(
'label' => esc_html__('Title', 'dede-designvox-divi-extension'),
'type' => 'text',
'description' => esc_html__('The title of this card', 'dede-designvox-divi-extension'),
'toggle_slug' => 'main_content',
),
'desc' => array(
'label' => esc_html__( 'Description', 'dede-designvox-divi-extension' ),
'type' => 'textarea',
'description' => esc_html__( 'The card description' ),
'toggle_slug' => 'main_content',
),
'src' => array(
'label' => esc_html__( 'Image', 'dede-designvox-divi-extension'),
'type' => 'upload',
'description' => esc_html__( 'Upload your desired image, or type in the URL to the image', 'dede-designvox-divi-extension'),
'upload_button_text' => esc_attr__('Upload an image', 'dede-designvox-divi-extension'),
'choose_text' => esc_attr__('Choose an Image', 'dede-designvox-divi-extension'),
'update_text' => esc_attr__('Set as Image', 'dede-designvox-divi-extension'),
'affects' => 'alt',
'toggle_slug' => 'main_content',
),
'alt' => array(
'label' => esc_html__( 'Image Alternative Text', 'dede-designvox-divi-extension' ),
'type' => 'text',
'depends_show_if' => 'on',
'depends_on' => array(
'src',
),
'description' => esc_html__( 'This defines the HTML ALT text. A short description of your image can be placed here.', 'dede-designvox-divi-extension' ),
'toggle_slug' => 'main_content',
),
'url' => array(
'label' => esc_html__( 'Link URL', 'dede-designvox-divi-extension' ),
'type' => 'text',
'description' => esc_html__( 'Enter the link that you would like this card to direct to.', 'dede-designvox-divi-extension' ),
'toggle_slug' => 'main_content',
),
);
}
}
new DEDE_Card_Item;
The shortcode is just being rendered as a string on the website, like:
[dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /]
You just have to leave out the line $this->no_render = true; and then implement the function render in the child Module.
I try to create the same module to add child items inside module. But I have another problem, when I click add child item I get the empty popup ((( like this https://prnt.sc/156jfev. I tried the same code as author
it's working for me you can try it,
Parents modules::
<?php
class GRDI_Barchart extends ET_Builder_Module {
public $slug = 'grdi_bar_chart';
public $vb_support = 'on';
protected $module_credits = array(
'module_uri' => '',
'author' => '',
'author_uri' => '',
);
public function init() {
$this->name = esc_html__( 'Bar Chart', 'grdi-graphina-divi' );
$this->plural = esc_html__( 'Bar Chart', 'et_builder' );
$this->slug = 'grdi_bar_chart';
$this->vb_support = 'on';
$this->child_slug = 'grdi_bar_chart_child';
$this->settings_modal_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => esc_html__( 'Graphina Text', 'dsm-supreme-modules-for-divi' ),
),
),
'advanced' => array(
'toggles' => array(
'separator' => array(
'title' => esc_html__( 'Separator', 'dsm-supreme-modules-for-divi' ),
'priority' => 70,
),
'image' => array(
'title' => esc_html__( 'Image', 'dsm-supreme-modules-for-divi' ),
'priority' => 69,
),
),
),
);
}
public function get_fields() {
return array(
'title' => array(
'label' => esc_html__( 'Title', 'grdi-graphina-divi' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Input your desired heading here.', 'simp-simple-extension' ),
'toggle_slug' => 'main_content',
),
'content' => array(
'label' => esc_html__( 'Description', 'grdi-graphina-divi' ),
'type' => 'tiny_mce',
'option_category' => 'basic_option',
'description' => esc_html__( 'Content entered here will appear inside the module.', 'grdi-graphina-divi' ),
'toggle_slug' => 'main_content',
),
);
}
public function get_advanced_fields_config() {
return array();
}
public function get_charts(){
echo "charts";
}
public function render( $attrs, $content = null, $render_slug ) {
wp_enqueue_script( 'graphina_apex_chart_script' );
wp_enqueue_style( 'graphina_apex_chart_style' );
wp_enqueue_style( 'graphina-public' );
echo "<pre/>";
$var = "<script>
var options = {
series: [{
name: 'Sales',
data: [4, 3, 10, 9, 29, 19, 22, 9, 12, 7, 19, 5, 13, 9, 17, 2, 7, 5]
}],
chart: {
type: 'bar',
height: 350
},
plotOptions: {
bar: {
borderRadius: 4,
horizontal: true,
}
},
dataLabels: {
enabled: false
},
xaxis: {
categories: ['South Korea', 'Canada', 'United Kingdom', 'Netherlands', 'Italy', 'France', 'Japan',
'United States', 'China', 'Germany'
],
}
};
var chart = new ApexCharts(document.querySelector('#charts'), options);
chart.render();
</script>" ;
$html = sprintf(
'<h1>%1$s</h1>
<p>%2$s</p>
<div id="charts"></div>',
$this->props['title'],
$this->props['content']
);
echo $html ;
echo $var ;
}
}
new GRDI_Barchart;
Child Modules::
<?php
class GRDI_BarchartChild extends ET_Builder_Module {
function init() {
$this->name = esc_html__( 'Bar Chart Items', 'et_builder' );
$this->plural = esc_html__( 'Bar Chart Items', 'et_builder' );
$this->slug = 'grdi_bar_chart_child';
$this->vb_support = 'on';
$this->type = 'child';
$this->child_title_var = 'title';
$this->no_render = true;
$this->settings_text = esc_html__( 'Bar Chart Settings', 'et_builder' );
$this->settings_modal_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => et_builder_i18n( 'Text' ),
),
),
'advanced' => array(
'toggles' => array(
'icon' => esc_html__( 'Icon', 'et_builder' ),
'toggle_layout' => esc_html__( 'Toggle', 'et_builder' ),
'text' => array(
'title' => et_builder_i18n( 'Text' ),
'priority' => 49,
),
),
),
);
}
function get_fields() {
$fields = array(
'child_title' => array(
'label' => et_builder_i18n( 'Data Label' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Data Label', 'et_builder' ),
'toggle_slug' => 'main_content',
'dynamic_content' => 'text',
'hover' => 'tabs',
'mobile_options' => true,
),
'child_content' => array(
'label' => et_builder_i18n( 'Data Value' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Data Value', 'et_builder' ),
'toggle_slug' => 'main_content',
'dynamic_content' => 'text',
'hover' => 'tabs',
'mobile_options' => true,
),
);
return $fields;
}
function render($attrs, $content, $render_slug){
$output = sprintf(
'<h1>%1$s</h1>
<p>%2$s</p>',
$this->props['child_title'],
$this->props['child_content']
);
return $output;
}
}
if ( et_builder_should_load_all_module_data() ) {
new GRDI_BarchartChild();
}
I was having similar issue to Mikael, where the child's setting looked like:
Just in case anyone is having similar issue, i realised its because the child's module wasn't being loaded. Since i was using a custom extension, there was a file called loader.phpin my extension that was responsible for loading the module files and it was loading the parent module file but skipping the child because of the pesky if statement with preg_match.
$module_files = glob( __DIR__ . '/modules/**/*.php' );
foreach ( (array) $module_files as $module_file ) {
if ( $module_file && preg_match( "/\/modules\/\b([^\/]+)\/\\1\.php$/", $module_file ) ) {
require_once $module_file;
}
}
if you replace it with:
foreach ( (array) $module_files as $module_file ) {
// if ( $module_file && preg_match( "/\/modules\/\b([^\/]+)\/\\1\.php$/", $module_file ) ) {
require_once $module_file;
//}
}
then it worked for me :)
Really hope that saves someone the 3 hours of debugging i spent.

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

How to add categori selection option in theme option?

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',

Categories