Function to Change Page Title In Wordpress - wordpress

I am using the below function to change the page title in wordpress. It is working on a single page (page.php), but is not working on my static home page or individual posts (single.php).
What do I need to change in order to make this work across the entire site?
<?php
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'wp_title', 'wpse46249_filter_wp_title' );
?>

function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'the_title', 'wpse46249_filter_wp_title' );
please note the title is saved in wp_posts table. The filter you used above will change all new posts titles being saved. This filter just modifys the title after pulling it from the db and doesn't actually change the db value.
Also will only work on pages where the_title() is called.

Related

Error with: WooCommerce Avoid add to cart for non logged user

I wondered if anyone can help me?
I am using a Wordpress site with Woocommerce plugin.
I am using a piece of code to avoid adding to cart for non logged in customers which I found on this site, it works great apart from one issue. It doesn't work on the product page. When you click the add to cart button, it doesn't redirect to the custom login page like it does if you press the button on the category view page. Instead the page just refreshes.
I put the code in to the functions.php file. I've then tried putting it into a few other places but that hasn't worked. Could anyone help me with this and let me know if there is another location I should be putting the code in? Thanks in advance, I'd really appreciate the help!
Here's the link to the question and the code is below: WooCommerce Avoid add to cart for non logged user
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Firstly, your function hook for woocommerce_loop_add_to_cart_link is incorrect. You are using conditionally_change_loop_add_to_cart_link rather than quantity_inputs_for_woocommerce_loop_add_to_cart_link.
Secondly, your URL for the link is using the current product page ID, which is going to point you at the current product page URL and not another page.
Other than that, you had it mostly correct with woocommerce_add_to_cart_validation.
EDIT:
For product single pages, if you look at content-single-product.php in Woocommerce, the action woocommerce_template_single_add_to_cart seems to handle what the "add to cart" form looks like. If you'd like to not show the add to cart form, you'll want to first remove the action from the woocommerce_single_product_summary hook.
if(!is_user_logged_in()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
Then add your own action in at that priority to put in your message:
add_action('woocommerce_single_product_summary', function() {
global $product_id;
if(!is_user_logged_in()) {
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
echo $message;
}
});
yes, you can do it by just adding following code into your active theme function.php file.
add_filter('woocommerce_get_price_html','login_before_addtocart');
function login_before_addtocart($price){
if(is_user_logged_in() ){
return $price;
}
else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
$response .= $price;
$response .= '<br> Login to add product into cart';
return $response;
}
}

Wordpress: map existing page content instead of a copy

I have a page with several subpages in wordpress. The page should display the same content as the second subpage. When I edit the page, there is an option to copy the content from the subpage into it, but then I would have to maintain two instances of the text. Is there a possibility to map existing page content into a page? I'm using Wordpress.com so I cannot edit any files/links on the server.
You could use a shortcode that fetches the content of a page specified. In functions.php:
add_action( 'init', 'so20477735_init', 11 );
function so20477735_init()
{
add_shortcode( 'duplicate_page', 'so20477735_shortcode_callback' );
}
function so20477735_shortcode_callback( $atts, $content = null )
{
extract( shortcode_atts( array(
'page_id' => 0
), $atts ) );
$page_data = get_page( $page_id );
$return = '';
if( ! is_null( $page_data ) )
$return .= $page_data->post_content;
return $return;
}
Example usage:
[duplicate_page page_id="12"]
EDIT
I missed the fact you're using wp.com. You should be able to do something similar with the display posts shortcode:
http://en.support.wordpress.com/display-posts-shortcode/

Wordpress Gallery Shortcode Pregreplace

Basically I need to remove the gallery shortcode from the Wordpress content, I'm using
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );
It is removing the gallery shortcode successfully, but also the paragraph tags which I need to keep. The idea is that I want to output everything in the content except the gallery.
You could use Wordpress strip_shortcode function.
Look at the example in the Codex.
You can create a filter that strips shortcodes:
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and call it when you need (in your template):
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
EDIT 1
Another way to get that (and answering you comment) you can use Wordpress apply_filters function in the content after remove the undesirables shortcodes.
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '', $content );
$content = apply_filters('the_content', $content );
echo $content;
But I would not recommend to you to do that. I think forcing your site to modify the content of a post could make that hard to understanding. Maybe you should work with Wordpress Excerpt and avoid any problem.
A link that helped me
to remove a shortcode or a particular list of shortcode you can use this code.
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* #global int $remove_shortcode
* #param type $shortcodes comma seprated string, array of shortcodes
* #return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );
Accepts single, comma seprated shortcode string or array of shortcodes.

Change the page title using a short code

I am coding a plugin and I use a short code to render a page content. I want to change the page title. I used this:
// Change the title of the page of the charts to add the current month.
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) {
$title .= ' ' . date('F Y');
return $title;
}
But it does that for all the posts and pages. Can I do that only for the pages that contains the short code I created ? I tried to do that, but it doesn't work.
add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
// Add the filter only in the short code function callback.
add_filter('the_title', 'charts_title', 10, 2);
// ... //
return $content;
}
Can someone please help me ?
I understand that the specifics of your set up may require checking for the Shortcode, but maybe a Custom Field could be used for that:
add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );
function charts_title_so_15312385( $title, $post_id )
{
// Admin area, bail out
if( is_admin() )
return $title;
// Custom field not set, bail out
$mod_title = get_post_meta( $post_id, 'mod_title', true );
if( !$mod_title )
return $title;
// Ok, modify title
$title .= ' ' . date( 'F Y' );
return $title;
}
The Shortcode could even "talk" with the Custom Field for extended configurations.
For ease of use, you could make a Custom Meta Box or use a plugin like Advanced Custom Fields.

Only show images/attachments within the gallery?

When i create a page, add a gallery, and browse this gallery on the front-end it will browse all the attachments that are associated with that page, instead of just the images within that gallery. Is there a way to filter all the other attachments and only show the images within a certain gallery? So that, for instance, when I delete the gallery and add a new gallery on the same page > only the new gallery is shown?
Any ideas?
This might not be the most elegant way, but i've found it very usefull.
Passing a post ID to the function below will load a gallery from the post_content of that post. So you would create a gallery and insert it into your post content, then in the template you run this function and will be returned with an array of attachments in that gallery which you are free to to whatever with, i.e slideshows and the likes.
function wp_load_gallery($post_id) {
$post = get_post( $post_id );
$regx = '/' . get_shortcode_regex() . '/';
preg_match( $regx, $post->post_content, $matches );
$ids = shortcode_parse_atts( $matches[3] );
$gallery = array( );
foreach( explode( ',', $ids['ids'] ) as $id ) {
if($id) {
$gallery[] = get_post( $id );
}
}
return $gallery;
}
Note that the shortcode is not cut from the content, so when you display the content you should run it through the strip_shortcodes function, i.e:
echo strip_shortcodes( get_the_content() );
This allows you to update the gallery whenever you want with whatever you want.
EDIT:
To simply display all images:
$gallery = wp_load_gallery($YOUR_POST_ID);
foreach($gallery as $image) {
echo wp_get_attachment_image($image->ID);
}

Resources