i need a trick about wordpress.
i want replace a word from my displaying post title at whole website.(not permantly or not on database. only show temporary at browser.)
for example: i want change post word to text
here is list my post titles:
1- This is first post title
2- This is second post title
3- This is third post title
4- This is fourth post title
here is my new post title list:
1- This is first text title
2- This is second text title
3- This is third text title
4- This is fourth text title
How can i do this?
in your functions.php:
function title_changer($title) {
global $post;
$title = str_replace('text','post',$post->post_title);
return $title;
}
add_action('the_title','title_changer');
go to your single.php in your theme path
find get_the_title or the_title, for each one you may change the title_changer function with the specific functions used in the single.php
you can do using Jquery, but you've to change few things as according to your HTML tags
h2.entry-title HERE you've to change your post title CSS class and I'm hoping there is an anchor tag inside heading, so I'm replacing the HTML of anchor tag, Please change accordingly and add in your functions.php file.
add_action( 'wp_footer', 'replace_title' );
function replace_title() { ?>
<script>
(function($) {
jQuery('h2.entry-title').each(function(index, value) {
let text = jQuery(this).find("a").html();
let result = text.replace("post", "W3Schools");
jQuery(this).find("a").html(result);
});
})(jQuery);
</script>
<?php
}
This is my approach I don't know how good it is but it works.
function menuarray() {
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ "primary" ] ) && !is_admin() ) {
$menu = wp_get_nav_menu_object($locations[ "primary" ]);
$menuitems = wp_get_nav_menu_items( $menu->term_id );
$idCats = array_column($menuitems, 'title');
return $idCats;
}
}
global $menutem;
$menutem = menuarray();
First I create this function globally in functions.php to I don't repeat it inside the function.
After that I create
add_filter('the_title', 'remove_header_metis', 10,2);
function remove_header_metis($title, $id) {
global $menutem;
if ( !is_admin() && ('post' == get_post_type($id) || 'page' == get_post_type($id) ) && !in_array($title, $menutem) ) {
$title = "";
}
return $title;
}
In this way, my menu stays ok and I change what I needed. You can tweak this for your needs.
Related
I have this php code and it displays a button in the single product page. Everything works fine but the button generated from the shortcode is displayed in EVERY product category.
I want to display the button from shortcode ONLY in a specific product category.
<?php
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
{
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
?>
I tried this but the site crashed
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
if ( in_category( $cat 'category-slug' ) ) {
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
Let's see if I can help you!
First of all you're missing two curly brackets in your function and that is why your site crashed. You need to wrap your function with the curly brackets to avoid this.
What I think you want to do is to only execute the shortcode if it's a certain product category archive page. You can do this by using if( is_product_category() ) You need to define the category either by slug, id or title.
Try this function and change "example" to the title of your category. Let me know how it works out!
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote() {
if( is_product_category('example') ) { //change "example" to category title, id or slug
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
}
I'm using an event management plugin, and I want to display some specific event details via shortcodes.
I've found this very useful guide regarding this plugin's shortcodes:
https://urjtechhelp.zendesk.com/hc/en-us/articles/115002801594-Embedding-Single-Events-with-the-tribe-event-inline-Shortcode
But I have one problem. As stated in the guide you must always supply an event’s ID via the id shortcode attribute, like this:
[tribe_event_inline id="167"]
What I'm trying to achieve is for the shortcode to always use the ID of the post it's placed in.
I've tried adding additional shortcode
add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );
function the_dramatist_return_post_id() {
return get_the_ID();
}
And then nest the shortcode in the original one, but apparently it doesn't work that way.
Any idea on how to achieve this?
Thank you in advance for any ideas.
Maybe it's help:
add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );
function the_dramatist_return_post_id() {
global $post;
return $post->ID ?? '';
}
This would not work using the WP text editor. It would also not be a clean implementation.
I would recommend going straight to the problem and edit the function responsible for the "tribe_event_inline" shortcode.
Try searching for add_shortcode('tribe_event_inline' in your source code files.
Here is one my function what I use time to time. I simplify it for you but with small work can be expanded to more levels.
function current_ID($post_id=NULL){
global $product, $page;
if(is_numeric($post_id) && $post_id == intval($post_id)) {} else {
// Get ID from the current object
if(!is_object($post_id)) {} else if(property_exists($post_id, 'ID')) {
$post_id = $post_id->ID;
}
// Woocommerce product page
if(empty($post_id) && property_exists($product, 'ID')) $post_id = $product->ID;
// Standard page/post ID
if(empty($post_id)) $post_id = get_the_ID();
// Get ID from the $page global
if(empty($post_id) && property_exists($page, 'ID')) $post_id = $page->ID;
// GET request
if(isset($_GET['post']) && is_numeric($_GET['post']) && $_GET['post'] > 0 && intval($_GET['post']) == $_GET['post']) $post_id = intval($_GET['post']);
if(isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] > 0 && intval($_GET['p']) == $_GET['p']) $post_id = intval($_GET['p']);
// Get ID from the get_queried_object_id() function
if(empty($post_id) && function_exists('get_queried_object_id')) $post_id = get_queried_object_id(); // Need tests
}
// The end
return $post_id;
}
It support also Woocommerce.
Now you need to include it into your project and add it to shortcode or combine with some other function to accept it.
Idea:
Use a template for your shortcode like [tribe_event_inline id="ZXZX"]
Add filter for the_content.
Use your filter to replace ZXZX with post ID.
I am a new developer and I just read in the WordPress Code Reference site that the_title() has been deprecated and get_the_category_by_ID should be used instead.
The thing is that I just can't find a way to replace it so that it successfully displays the title on each blog post. In the examples it seems to be used only for the categories.
Can anybody give me an example of how to do this properly, please?
You can replace the title using a wp hook. You can put this code, or inside the function.php on your theme; or in your plugin example:
function my_custom_replace_title( $title, $id = null ) {
/* Put here your condition, you get the post ID on "$id", and the current title on "$title"*/
// by category
//if ( in_category('XXX', $id ) ) {
// return '';
//}
//if ($title === 'custom text' ){
// return 'this is my new title';
// }
$title = 'My Custom Title - Forced'; // This is to test it.
return $title;
}
add_filter( 'the_title', 'my_custom_replace_title', 10, 2 );
I recommended to read https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
I hope help you.
How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}
How do I modify a page title for specific pages in shortcode?
The following will change the title but it executes for every page. I need more control over where it executes.
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
Is there a way to call the above in a shortcode function? I know how to use do_shortcode() but the above is a filter.
My goal is to modify the page title based on a URL parameter. This only happens for specific pages.
Although WordPress shortcodes was not designed to do this, it can be done. The problem is shortcodes are processed AFTER the head section is sent so the solution is to process the shortcode BEFORE the head section is sent.
add_filter( 'pre_get_document_title', function( $title ) {
global $post;
if ( ! $post || ! $post->post_content ) {
return $title;
}
if ( preg_match( '#\[mc_set_title.*\]#', $post->post_content, $matches ) !== 1 ) {
return '';
}
return do_shortcode( $matches[0] );
} );
add_shortcode( 'mc_set_title', function( $atts ) {
if ( ! doing_filter( 'pre_get_document_title' ) ) {
# just remove the shortcode from post content in normal shortcode processing
return '';
}
# in filter 'pre_get_document_title' - you can use $atts and global $post to compute the title
return 'MC TITLE';
} );
The critical point is when the filter 'pre_get_document_title' is done the global $post object is set and $post->post_content is available. So, you can find the shortcodes for this post at this time.
When the shortcode is normally called it replaces itself with the empty string so it has no effect on the post_content. However, when called from the filter 'pre_get_document_title' it can compute the title from its arguments $atts and the global $post.
Taken from the WordPress Codex
Introduced in WordPress 2.5 is the Shortcode API, a simple set of
functions for creating macro codes for use in post content.
This would suggest that you can't control page titles using shortcodes as the shortcode is run inside the post content at which point the title tag has already been rendered and is then too late.
What is it exactly that you want to do? Using the Yoast SEO Plugin you can set Post and Page titles within each post if this is what you want to do?
You could create your custom plugin based on your URL parameters as so:
function assignPageTitle(){
if( $_GET['query'] == 'something' ) { return 'something'; }
elseif( $_GET['query'] == 'something-else' ) { return 'something-else'; }
else { return "Default Title"; }
}
add_filter('wp_title', 'assignPageTitle');