Retrieving data from additional OptionTree pages - wordpress

Adding OptionTree pages is simple with the code below, but does anyone know how to retrieve the stored data?
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
I tried this $my_plugin_options = get_option('custom_options'); but it only shows the word 'array' on the front end?

This is how to retrieve the stored data:
$my_plugin_options = get_option('custom_options');
echo $my_plugin_options['test_section_input'];

Related

how can i add post-categories list in elementor custom widget

My control code is
i want to create control in elementor which shows all post categories. please help me how can i achieve this...
enter image description here
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => [
$category,
],
]
); ```
You can use WP get_categories() to get all categories. check below code.
$options = array();
$args = array(
'hide_empty' => false,
);
$categories = get_categories($args);
foreach ( $categories as $key => $category ) {
$options[$category->term_id] = $category->name;
}
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $options,
]
);

Hide/display a link in BuddyPress toolbar based on user role

How do I hide/display a link in BuddyPress toolbar if the user has a particular role?
function your_bp_admin_bar_add() {
global $wp_admin_bar, $bp;
if ( !bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) )
return;
$user_domain = bp_loggedin_user_domain();
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
}
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}
add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_add', 300 );
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor. It is supposed to display Create Course.
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor.
Are you sure that the role / capability for instructors is lp_teacher?
If you want to only show Create Course to instructors, then try adding an else:
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
} else {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}

Wordpress add Shortcut link to Enable/Disable plugin in admin bar

I would like to add a link to enable and disable a plugin in the admin bar.
Below the code, I wanted to know if it is the correct method.
thank you
function custom_admin_bar_link( $admin_bar ) {
$admin_bar->add_menu( array(
'id' => 'wp-custom-link',
'title' => 'Custom Item',
'href' => '',
'meta' => array(
'title' => __('Custom'),
),
));
$admin_bar->add_menu( array(
'id' => 'disable-link',
'parent'=> 'wp-custom-link',
'title' => 'Disable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Disable Amazon'),
),
));
$admin_bar->add_menu( array(
'id' => 'unable-link',
'parent'=> 'wp-custom-link',
'title' => 'Enable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Enable Amazon'),
),
));
}
add_action( 'admin_bar_menu', 'custom_admin_bar_link', 100 );

Unable to add custom select field in woocommerce

I want to add a custom field options to checkout page. I am using the following code :
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'option_a' => __('option a', 'woocommerce' ),
'option_b' => __('option b', 'woocommerce' )
)
);
I want to show the options (option_a,option_b) from database or
I want to use dynamic data and want to use for loop in the options menu
How can I use for loop inside this function ?
Just do it before, like this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$args = array(
'post_type' => array('options'),
'posts_per_page' => -1
);
$posts = new WP_Query($args);
$options = array();
foreach ($posts as $post) {
$options[$post->ID] => attr_esc($post->post_title);
}
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => $options
);
return $fields;
}

WP Theme Customizer - options order

i've got a problem with theme customizer. My code is:
function candyfloss_theme_customizer( $wp_customize ) {
class Heading extends WP_Customize_Control {
public $type = 'heading';
public function render_content() {
?>
<label>
<span class="customize-control-title" style="border-bottom: 1px dashed #666;"><strong><?php echo esc_html( $this->label ); ?></strong></span>
</label>
<?php
}
}
$wp_customize->add_setting('products_heading', array(
'default',
) );
$wp_customize->add_control(new Heading ($wp_customize, 'products_heading', array(
'label' => __('Home - products section'),
'type' => 'heading',
'section' => 'home',
) ) );
$wp_customize->add_setting('candyfloss_product_first', array(
'deafault',
) );
$wp_customize->add_control('candyfloss_product_first', array(
'label' => __('First product page'),
'type' => 'dropdown-pages',
'section' => 'home',
) );
$wp_customize->add_setting('candyfloss_product_second', array(
'deafault',
) );
$wp_customize->add_control('candyfloss_product_second', array(
'label' => __('Second product page'),
'type' => 'dropdown-pages',
'section' => 'home',
) );
$wp_customize->add_setting('candyfloss_product_third', array(
'deafault',
) );
$wp_customize->add_control('candyfloss_product_third', array(
'label' => __('Third product page'),
'type' => 'dropdown-pages',
'section' => 'home',
) );
};
add_action( 'customize_register', 'candyfloss_theme_customizer', 11 );
And the problem is in order of this. At admin panel view is
second option,
first option,
heading,
third option,
Can anyone know, what I'm doing wrong? Could You help me? I'll be thankful
I found the answer. Wordpress gives a random priority to controls. To solve it we just need to add priority number to each control.
eg.:
$wp_customize->add_control(new Heading ($wp_customize, 'products_heading', array(
'label' => __('Home - products section'),
'type' => 'heading',
'section' => 'home',
'priority' => 2,
) ) );

Resources