I would like to create two plugins for my wordpress blog and I need different admin settings for both.I am trying to adding plugin administration; but which selecting both settings together when select single.What's wrong with me.Here I am attaching what I done
add_action('admin_menu', 'sb_load_featured_slider');
function sb_load_featured_slider() {
$mypage = add_options_page('SB_Featured Slider', 'SB_Featured Slider', 8, array('sbslider','sbslider1','sbslider2'), 'sb_featured');
add_action( "admin_print_scripts-$mypage", 'sb_loadjs_admin_head' );
}
and my second plugin says as follows
add_action('admin_menu', 'sb_load_intro');
function sb_load_intro()
{
$mypage = add_options_page('SB Introduction', 'SB Introduction', 9, array('sbintro','sbjquery'), 'sb_intro');
add_action( "admin_print_scripts-$mypage", 'sb_introjs_admin_head' );
}
well I see one similarity between the 2 functions. You use the $mypage variable in both functions and use that for the second add action. try to use different variable names and see if it fix your problem.
eg: $mypage1, $mypage2
Related
I know there are other questions like this but didn't find a reliable answer. So:
First activate the thing (simplyfied code):
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
Second, delete title tag from header.php.
Third, on page templates, before calling get_header(), add something like this:
add_filter('wp_title', 'set_custom_title', 10, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
Well, this is not working at all, in any template, being a page, an archive, a custom taxonomy or post type archive. No nothing. Wordpress is generating titles by itself.
Why? Am I doing something wrong? Note that this code once upon a time just worked: used in other sites/themes.
Is it maybe an issue of wp5.2.0?
So, thanks to #Vel, the answer is to re-add the title tag (even if in previous wp versions > don't know til what version you had to delete it form head instead).
Current working code for me:
//functions.php
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
//header.php
<title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
//page templates
$window_title = // do something
add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
Try to use follows code -
add_filter('document_title_parts', function($titles){
return array('title' => 'Custom Title');
});
For anyone still having this issue of the wp_title filter not working, I'd suggest adding a higher priority value. The higher priority value will ensure that your filter is executed and not overriden by other filters in your theme or plugins installed. Please see below: (ref: https://developer.wordpress.org/reference/functions/add_filter/)
// the 9999999 priority value will force this filter to be executed closer to the end. A lower number corresponds with earlier execution
add_filter('wp_title', 'set_custom_title', 9999999, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
In my case Yoast SEO was changing the way title was rendered and only the following worked:
function filter_lp_title($title) {
return 'New title';
}
add_filter( 'pre_get_document_title', 'filter_lp_title', 25 );
Task:
To allow CMS users to change the image used when sharing a post to Twitter. Yoast uses the Featured Image when creating the Twitter Card.
The approach:
Add a custom meta field to posts. Extend the WPSEO_Twitter class and or just the private function output_metatag(). If the custom meta field is not empty, use the custom field value instead of the default.
Code:
if (class_exists('WPSEO_Twitter')) :
remove_action( 'wpseo_head', array( 'WPSEO_Twitter', 'get_instance' ), 40 );
add_action( 'wpseo_head', array( 'EXAMPLE_WPSEO_Twitter', 'get_instance' ), 40 );
class EXAMPLE_WPSEO_Twitter extends WPSEO_Twitter {
// etc
}
endif;
Issues:
The remove_action isn't working and the twitter meta code is being duplicated. The plugin's class and my extended class are both being executed.
Links: https://github.com/Yoast/wordpress-seo/blob/trunk/frontend/class-twitter.php
I think it would be easier to add a filter to wpseo_twitter_image that changes the image to what you desire.
Something along the lines of
add_filter("wpseo_twitter_image", function($img) {
if($myimg = get_post_meta(get_the_ID(), "custom-twitter-image", true)) {
return $myimg;
}
return $img;
});
should probably work for you, if I understood you correctly.
So I want to include fusion builder's post editor on some custom post types that I've created. There is a fix to this go to fusion-core > admin > class-pagebuilder.php and edit line 53.
var $allowed_post_types = array('page','post','avada_faq','avada_portfolio', 'add my custom types here');
But anytime there is an update this will be deleted and I'd like to not have to worry about this, or worry about it! So is there anyway i can create a plugin helper, or add something to my functions.php file that wont get replaced every time there is an update.
the instance is create line 22 of the file fusion-core.php
so to overide this, you can try something like this :
add_action ("plugins_loaded", function () {
// unregister habitual call
remove_action( 'plugins_loaded', array( 'Fusion_Core_PageBuilder', 'get_instance' ) );
// call of the instance
// you have to change the path of the fusion-core plugin
if( ! get_option( 'avada_disable_builder' ) ) {
if ( is_admin() ) {
require_once( path of the fusion-core plugin . 'admin/class-pagebuilder.php' );
$instance = Fusion_Core_PageBuilder::get_instance();
$instance->allowed_post_types[] = "custom post type";
}
}
}, 9); // priority 9 to be called before the line of fusion-core.php
For me, I found that there was no need to remove the action...and in fact, it did not work when I did so. Instead, I simply called the same action and changed the values. I also found that I only needed the "UI instance". Here's what worked for me. I've now added the page builder successfully to ten different post types on my site see below:
if ( is_admin() ) {
require_once('path to plugin directory/fusion-core/admin/class-pagebuilder.php' );
require_once('path to plugin directory/fusion-core/admin/page-builder/classes/class-ui.php' );
$ui = Fusion_Core_PageBuilder_UI::get_instance();
$ui->settings['allowed_post_types'][] = 'your_custom_post_type';
}
I want to add an additional menu item to my navigation via my functions.php
The code below works, but how can I add the CSS classes which are used in the navigation to the li element automatically without writing each class manually in the below stated function= There must be something like get_classes_for_li_elements() - does someone know how I can achieve this?
Thanks!
add_filter( 'wp_nav_menu_home_items', 'add_itemcart_to_menu' , 10, 2 );
function add_itemcart_to_menu( $items, $args ) {
$menu_item_li = '<li>My Item</li>';
return $items . $menu_item_li;
}
I would write something like
$menu_item_li = 'My Item';
or asign a class used by your theme
I am having trouble trying to pass an extra variable in the url to my WordPress installation.
For example /news?c=123
For some reason, it works only on the website root www.example.com?c=123 but it does not work if the url contains any more information www.example.com/news?c=123. I have the following code in my functions.php file in the theme directory.
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
Any Ideas?
To make the round trip "The WordPress Way" on the "front-end" (doesn't work in the context of wp-admin), you need to use 3 WordPress functions:
add_query_arg() - to create the URL with your new query variable ('c' in your example)
the query_vars filter - to modify the list of public query variables that WordPress knows about (this only works on the front-end, because the WP Query is not used on the back end - wp-admin - so this will also not be available in admin-ajax)
get_query_var() - to retrieve the value of your custom query variable passed in your URL.
Note: there's no need to even touch the superglobals ($_GET) if you do it this way.
Example
On the page where you need to create the link / set the query variable:
if it's a link back to this page, just adding the query variable
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">
if it's a link to some other page
<a href="<?php echo esc_url(
add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) )
)?>">
In your functions.php, or some plugin file or custom class (front-end only):
function add_custom_query_var( $vars ){
$vars[] = "c";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
On the page / function where you wish to retrieve and work with the query var set in your URL:
$my_c = get_query_var( 'c' );
On the Back End (wp-admin)
On the back end we don't ever run wp(), so the main WP Query does not get run. As a result, there are no query vars and the query_vars hook is not run.
In this case, you'll need to revert to the more standard approach of examining your $_GET superglobal. The best way to do this is probably:
$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );
though in a pinch you could do the tried and true
$my_c = isset( $_GET['c'] ) ? $_GET['c'] : "";
or some variant thereof.
There are quite few solutions to tackle this issue. First you can go for a plugin if you want:
WordPress Quickie: Custom Query String Plugin
Or code manually, check out this post:
Passing Query String Parameters in WordPress URL
Also check out:
add_query_arg
Since this is a frequently visited post i thought to post my solution in case it helps anyone. In WordPress along with using query vars you can change permalinks too like this
www.example.com?c=123 to www.example.com/c/123
For this you have to add these lines of code in functions.php or your plugin base file.
From shankhan's anwer
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'c'; // c is the name of variable you want to add
return $vars;
}
And additionally this snipped to add custom rewriting rules.
function custom_rewrite_basic()
{
add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
For the case where you need to add rewrite rules for a specifc page you can use that page slug to write a rewrite rule for that specific page. Like in the question OP has asked about
www.example.com/news?c=123 to www.example.com/news/123
We can change it to the desired behaviour by adding a little modification to our previous function.
function custom_rewrite_basic()
{
add_rewrite_rule('^news/([0-9]+)/?', 'news?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
Hoping that it becomes useful for someone.
add following code in function.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'var1'; // var1 is the name of variable you want to add
return $vars;
}
then you will b able to use $_GET['var1']
<?php
$edit_post = add_query_arg('c', '123', 'news' );
?>
Go to New page
You can add any page inplace of "news".
One issue you might run into is is_home() returns true when a registered query_var is present in the home URL. For example, if http://example.com displays a static page instead of the blog, http://example.com/?c=123 will return the blog.
See https://core.trac.wordpress.org/ticket/25143 and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/ for more info on this.
What you can do (if you're not attempting to affect the query) is use add_rewrite_endpoint(). It should be run during the init action as it affects the rewrite rules. Eg.
add_action( 'init', 'add_custom_setcookie_rewrite_endpoints' );
function add_custom_setcookie_rewrite_endpoints() {
//add ?c=123 endpoint with
//EP_ALL so endpoint is present across all places
//no effect on the query vars
add_rewrite_endpoint( 'c', EP_ALL, $query_vars = false );
}
This should give you access to $_GET['c'] when the url contains more information like www.example.com/news?c=123.
Remember to flush your rewrite rules after adding/modifying this.
to add parameter to post urls (to perma-links), i use this:
add_filter( 'post_type_link', 'append_query_string', 10, 2 );
function append_query_string( $url, $post )
{
return add_query_arg('my_pid',$post->ID, $url);
}
output:
http://yoursite.com/pagename?my_pid=12345678
This was the only way I could get this to work
add_action('init','add_query_args');
function add_query_args()
{
add_query_arg( 'var1', 'val1' );
}
http://codex.wordpress.org/Function_Reference/add_query_arg
In your case, Just add / after url and then put query arguments. like
www.example.com/news/?c=123 or news/?c=123
instead of
www.example.com/news?c=123 or news?c=123