I am looking for a method to have a Admin-bar custom link open in a popup.
The code I have is as follows:
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'new-content',
'id' => 'custom_link',
'title' => __('My custom link'),
'href' => 'http://www.google.com'
) );
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
Which results in:
It is still unclear to me however how I can have this specific link open (ideally) as a JavaScript popup or eventually in a new window.
Some advice would be very much appreciated.
Thank you very much,
Patrick
By adding meta information to your code will open the link in a new tab.
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'new-content',
'id' => 'custom_link',
'title' => __('My custom link'),
'href' => __('http://www.google.com'),
'meta' => array(
'target' => '_blank',),
) );
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
Cheers!!!
Related
I want to add default image options in WordPress admin (we can say set default options in image field ) when I try to create an image field using ACF thank you so much in advance
You can put the code like below in your functions.php
add_action('acf/render_field_settings/type=image', 'add_defult_image_field');
function add_defult_image_field($field) {
acf_render_field_setting( $field, array(
'label' => 'Defult Image',
'instructions' => 'Appears when creating a new post',
'type' => 'image',
'name' => 'defult_value',
));
}
Taken from https://stupop010.medium.com/how-to-set-a-default-image-on-acf-wordpress-977d38869b78, you need to create an action to a default_value field. After that, add a filter that checks when the user did not select any image, and then loads the default value.
add_action('acf/render_field_settings/type=image', function ($field) {
acf_render_field_setting( $field, array(
'label' => __('Default Image','acf'),
'instructions' => __('Appears when creating a new post','acf'),
'type' => 'image',
'name' => 'default_value',
));
});
add_filter('acf/load_value/type=image', function ($value, $post_id, $field) {
if (!$value) {
$value = $field['default_value'];
}
return $value;
}, 10, 3);
I'm trying to retrieve some customizer settings that I initiated.
function notop_customize_register( $wp_customize ) {
$wp_customize->add_section( 'header_images', array(
'title' => 'Header Images',
'priority' => 2,
'description' => "Add images with links to be redirected to when the images are clicked",
) );
// Add color scheme setting and control.
$wp_customize->add_setting( 'header_image_1_setting', array(
'capability' => 'edit_theme_options',
'type' => 'option',
) );
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'header_image_1_control', array(
'label' => __( 'Import Image', 'notop' ),
'section' => 'header_images',
'settings' => 'header_image_1_setting',
) ) );
}
add_action( 'customize_register', 'notop_customize_register' );
How do I get the url of header_image_1?
I thought it might be get_theme_mod('header_image_1_setting'); but that hasn't worked so far. Also, I'm not sure if I'm actually supposed to call the customize_register file in my index somewhere or if it's okay the way it is.
cale_b's comment is the answer.
get_option('header_image_1_setting')
=> http://...
I'm trying to apply a template_include filter to inflate a template from my plugin.
I've seen a lot of people using the following with success:
function include_template_files($template_file) {
global $post;
$plugindir = dirname( __FILE__ );
if ('mycustomposttype' == get_post_type()){
$templatefilename = 'mytemplate.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
return $template_file;
}
add_filter( 'template_include', 'include_template_files' );
get_post_type() returns empty, and $post is not instantiated for my custom post. This works fine for WP types (posts, pages, etc.).
What am I doing wrong?
I'm on WP 3.7.1 and I'm using the default twenty thirteen theme.
UPDATE:
I register my type this way:
function register_mycustom_post_type() {
register_post_type( 'mycustomposttype', array(
'labels' => array(
'name' => 'My posts',
'singular_name' => 'My post',
'menu_name' => 'My posts',
'add_new' => 'New custom post',
'add_new_item' => 'Add new custom post',
),
'public' => true,
'show_ui' => true,
'menu_icon' => plugins_url('myicon.png',__FILE__),
'supports' => array( 'title' ,'thumbnail', 'editor' ),
) );
}
add_action('init','register_mycustom_post_type');
SECOND UPDATE (PARTIAL SOLUTION):
The problem is given by permalinks rewrite. If I use the default URLs (index.php?...) it works fine.
SOLUTION
Adding the right rewrite options to my post type solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Try using the get_queried_object() function like this
$queried = get_queried_object();
print_r($queried);
You should get and object with information from the current page
I answer myself. The problem was permalink rewrite.
Adding the following to register_post_type options solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Sometimes this can work as well:
get_query_var( 'post_type' );
I have just registered a new Custom Taxonomy in Wordpress (as per the docs). Here is a copy of the code in functions.php for reference:
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'people_init' );
As you can see in the image below, the Taxonomy appears within left-hand navigation but when my (admin) user clicks on the option I am displayed with an You are not allowed to edit this item. error:
Can anyone suggest why this may be?
Almost as soon as I posted this I realised it is the capabilities array. Removing this, so that it reverts to the default allows access as intended.
After further investigation I found that the following was the best settings to get this functioning correctly:
'capabilities' => array(
'manage__terms' => 'edit_posts',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts'
)
I have this little strange problem ;). I would like to load custom scripts (css, js) only when an admin's custom post type editor is loaded (adding new custom post type, editing custo mpost type).
Custom post types are registered this way:
add_action( 'init', 'cs_product_register_post_types' );
function cs_product_register_post_types() {
$product_args = array(
'public' => true,
'rewrite' => array(
'slug' => 'products',
'with_front' => false,
'pages' => false
),
'supports' => array(
'title',
'editor',
'page-attributes'
),
'labels' => array(
'name' => 'Produkty',
'singular_name' => 'Produkt',
'add_new' => 'Dodaj nowy produkt',
'add_new_item' => 'Dodaj nowy produkt',
'edit_item' => 'Edytuj produkt',
'new_item' => 'Nowy produkt',
'view_item' => 'Wyswietl produkt',
'search_items' => 'Wyszukaj produkt',
'not_found' => 'Produktu nie znaleziono',
'not_found_in_trash' => 'Brak usuniętych produktów'
),
'menu_position' => 3,
);
register_post_type( 'products', $product_args );
}
For those there is a function registering custom metabox:
add_action( 'add_meta_boxes', 'cs_products_mb_create' );
function cs_products_mb_create() {
//create a custom meta box
add_meta_box( 'products-info', 'Ustawienia Produktu', 'cs_products_mb_function', 'products', 'normal', 'high' );
}
That works only for registered custom post type (products).
Now the only thing is to load custom js. It can be done this way:
add_action('admin_print_styles-post.php', 'cs_products_admin_styles');
add_action('admin_print_styles-post-new.php', 'cs_products_admin_styles');
function cs_products_admin_styles() {
wp_enqueue_style( 'thickbox' );
wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '', '1.0');
}
But it works for all posts, and its not a best way to do it ;).
Thanks for any ideas.
edit
After digging, and digging, and digging... One of simplest ways to do it:
//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) ) {
add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}
function cs_admin_customposttype_scripts(){
wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');
wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}
But the problem with this solution is, that it works only when new custom post is being created. When editing, $_GET['post_type'] or $post_type are not available.
After searching a little bit more...
//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) || ( isset($_GET['post']) && get_post_type($_GET['post']) == 'products' ) ) {
add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}
function cs_admin_customposttype_scripts(){
wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');
wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}
Aditional or, and use of only one known variable - postID that is sent with $_GET and using get_post_type function did the job.