I want to execute a function in a specific page only, that edits the post title of posts inside a post-grid plugin.
I tried:
if (is_page('my-slug')) { add_filter(...); } //page slug
if (is_page('12345')) { add_filter(...); } //page ID
add_filter('the_title', 'title_with_attributes', 10, 2);
function title_with_attributes($title, $id) {
if (is_page('12345') || is_page('my-slug')) {
//Do stuff ...
}
}
With no results. The function is the following and runs fine.
function title_with_attributes($title, $id) {
global $wp_query;
if(get_post_type($post->ID)=="product"){
global $post;
//get other variables from post here ...
$title = "add text and vars ".$title;
}
return $title;
}
Maybe there is a way to add this function when the plugin runs but I do not know how. The plugin is the "Post-Grid" from WP Bakery
Related
I am trying to customise a title for a Wordpress page. In the page I have some php to obtain data from the database. I would like to output a custom title based on the data and the page id. I have tried this:
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title() {
global $post;
$postid = $post->ID;
if ($postid == '72616') {
$title['title'] = "Property number $propertyid";
return $title;
}
}
How can I check the postid and if it is the right page, output the data from the sql query?
Many thanks,
Neil.
The title is passed in for that filter. Make sure you return title outside of the if statement.
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title($title) {
global $post;
$propertyid = get_query_var('reference');
$postid = $post->ID;
if ($postid == '72616') {
$title = "Property number $propertyid";
}
return $title;
}
add_filter( 'query_vars', 'add_property_query_vars' );
function add_property_query_vars( $query_vars ) {
$query_vars[] = 'reference';
return $query_vars;
}
I'm creating a plugin OOP for wordpress. The plugin creates a new custom post type called teams. Within a team page a could use the shortcode [program] to generate some predefault html code. Also i've created custom fields with new meta boxes.
The problem however is: when i'm entering the page thats calling the plugin, equal the team page with the sortcode i need to get post id within my plugin to retrieve get_post_meta().
I've tried the following things:
public function __construct(){
// not working
$post;
$post->ID;
// not working
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
// not workiing
echo '<pre>';
print_r('post_id:' . get_the_ID());
echo '</pre>';
}
How could i receive the custom post id within my plugin when i visited the page from frontend (so the plugin is called, runs the shortcode)
My main class gets loaded like this:
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
run_plugin();
Within MyPlugin the constructor looks like
public function __construct() {
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
$this->version = PLUGIN_NAME_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'MyPlugin';
if(!empty(get_option($this->plugin_name))){
$this->clientID = get_option($this->plugin_name)['client_id'];
}
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_shortcodes();
}
If your plugin constructor is getting called too early, the post data won't be set up and ready to use.
You'll need to hook into one of WPs actions that run after everything is ready. The init action should be enough for the post data, but depending on what else you need you can hook into wp_loaded, as it doesn't run until after WordPress is fully loaded.
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
/* run_plugin(); */ // <- instead of this....
add_action( 'wp_loaded','run_plugin' ); // <- ...do this
Try to define post as global like
global $post
I have a bunch of pages, which are paginated using the url parameter ?page=
I am using wordpress and would like each page to have a custom made title. So far I've created a custom template an set up the following code:
$titletags = array('Title1','Title2');
if ($page==2) {$arrayno = 1}
function ChangeTitle($title) {
$title = $titletags[$arrayno];
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
I run this before the call to get the header. Now this code is not working, although it should change the title to Title2, when the url parameter is 2.
The code works if don't use the array inside the function, i.e. I do:
function ChangeTitle($title) {
$title = 'SomeTitle';
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
You have not access to $titletags array. first use global then get it. try this code:
$titletags = array('Title1', 'Title2');
function ChangeTitle( $title ) {
global $titletags, $page;
if ( $page == 2 ) {
return $titletags[1];
}
return $title;
}
add_filter( 'wpseo_title', 'ChangeTitle' );
Thanks for any help with this.
I need to know how to hook wp_insert_post (or whatever is similar and better?) without it firing multiple times. What is the correct way to do this in WordPress?
For example:
In Plugin.php
add_filter( 'wp_insert_post', 'add_data') );
...
function add_data()
{
// This line is outputted twice
terseErrorLog("This code was executed.");
}
Try this:
function add_data() {
global $post;
if ($post - > post_status == "publish") {
terseErrorLog("This code was executed.");
}
}
add_action('save_post', 'add_data');
<?php
/*
Plugin Name: Members
*/
function myFilter2($query)
{
if ($query->is_category)
{
$currently_listing_categories = $query->query_vars['category__in'];
print_r($currently_listing_categories);
}
}
add_filter('pre_get_posts','myFilter2');
?>
This plugin display the category ids when the url is not SEO friendly
http://domain.com/wplab/wpla4/?cat=4
. but when I turn on SEO
http://domain.com/wplab/wpla4/category/members/
the array is empty
how can I get the category id with SEO friendly urls
Use this function to get current cateogry in wp :
function getCurrentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
echo getCurrentCatID();
Just found for you also try this,
if(isset($wp_query->get_queried_object()->cat_ID))
{
$cur_catId = $wp_query->get_queried_object()->cat_ID;
}
if(isset($wp_query->get_queried_object()->ID))
{
$cur_postId = $wp_query->get_queried_object()->ID;
}
Paste in your functions.php or use in your plugin
add_filter('pre_get_posts','myFilter2');
function myFilter2()
{
global $wp_query;
$cat_name= $wp_query->query_vars['name'];
$cat_id=get_cat_id($cat_name);
echo $cat_id; // the category id will be available, echo is for testing only
}
When url is like http://example.com/category_name