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' )
) );
}
Related
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 );
I'm experiencing difficulties with a theme I developed.
I'm using Envato, where I cannot enter CSS anywhere other than wp_add_inline_style(); or enqueue style.
I use preview customization directly and add partial refresh.
I have a settings menu with the following options:
choice of layout A or B
choice of layout background color
The problem is that the layout changes when the background color is changed.
Does anyone have a solution?
Code snippet: customizer.php
function customizer_nav( $wp_customize ) {
// Section
$wp_customize->add_section( 'navigasi_sett',
array(
'title' => __( 'Navigasi Primary', 'mocita' ),
'priority' => 100,
'panel' => 'primary_panel',
)
);
// Background
$wp_customize->add_setting( 'nav_background',
array(
'default' => '#0663cc',
'transport' => 'postMessage',
'sanitize_callback' => 'sanitize_hex_color',
)
);
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'nav_background',
array(
'label' => __( 'Background Menus', 'mocita' ),
'section' => 'navigasi_sett',
'settings' => 'nav_background',
)
));
// Navigasi layout
$wp_customize->add_setting( 'nav_layout',
array(
'default' => 'tes1',
'transport' => 'postMessage',
'sanitize_callback' => 'mocita_sanitize_radio_img',
)
);
$wp_customize->add_control( new Mocita_Image_Radio( $wp_customize, 'nav_layout',
array(
'label' => __( 'Navigasi Layout', 'mocita' ),
'section' => 'navigasi_sett',
'choices' => array(
'tes1' => array(
'image' => trailingslashit( get_template_directory_uri() ) . 'images/navigasi/tes1.png',
'name' => __( 'tes1', 'mocita' )
),
'tes2' => array(
'image' => trailingslashit( get_template_directory_uri() ) . 'images/navigasi/tes2.png',
'name' => __( 'tes2', 'mocita' )
)
)
)
));
}
// selevtive_refresh
if ( isset( $wp_customize->selective_refresh ) ) {
// Navigasi Layout
$wp_customize->selective_refresh->add_partial( 'nav_layout',
array(
'selector' => '#primary',
'container_inclusive' => false,
'render_callback' => 'nav_layout',
)
);
}
// callback nav_layout
function nav_layout() {
$layout = get_theme_mod( 'nav_layout', 'nav1' );
if ( $layout === 'nav1' ) {
get_template_part( 'template-parts/navigasi/navigasi', 'primary1' );
} elseif ( $layout === 'nav2' ) {
get_template_part( 'template-parts/navigasi/navigasi', 'primary2' );
}
}
?>
customize-preview.js
(function( $ ) {
// ( Navigasi ) Background
wp.customize( 'nav_background', function( value ) {
value.bind( function( to ) {
$( '#menu' ).css( 'background', to );
});
});
} )( jQuery );
I don't understand how the genesis_markup function turns:
array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
into
"<div class="widget-area">"
I do not understand why the id is not used in the output at all and the aside class is also not used....I would have thought that it should be.....
When I do this in the code:
$before_markup_args = array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
d($before_markup_args);
d(genesis_markup( $before_markup_args ));
First, I would like to confirm the missing space:
```
$before_markup_args = array(
'open' => '<aside class="widget-area info-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
```
"Onder" is Dutch for "Below". Thus, "info-onder-content" means "info-below-content". Possibly a title for the widget area.
This is a code example that is used for the genesis_widget_area function, typically found in a functions.php file in a WordPress Genesis child theme.
$widget_area_args = array('before' => genesis_markup($before_markup_args),
);
genesis_widget_area($id, $widget_area_args);
We can understand how this works by looking up the definition of the genesis_widget_area function in the /var/www/html/wp-content/themes/genesis/lib/functions/widgetize.php file.
function genesis_widget_area( $id, $args = array() ) {
if ( ! $id ) {
return false;
}
$defaults = apply_filters(
'genesis_widget_area_defaults',
array(
'before' => genesis_markup(
array(
'open' => '<aside class="widget-area">' . genesis_sidebar_title( $id ),
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
),
)
),
'after' => genesis_markup(
array(
'close' => '</aside>',
'context' => 'widget-area-wrap',
'echo' => false,
)
),
'default' => '',
'show_inactive' => 0,
'before_sidebar_hook' => 'genesis_before_' . $id . '_widget_area',
'after_sidebar_hook' => 'genesis_after_' . $id . '_widget_area',
),
$id,
$args
);
$args = wp_parse_args( $args, $defaults );
if ( ! $args['show_inactive'] && ! is_active_sidebar( $id ) ) {
return false;
}
I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}
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'];