Right now I am using this function to redirect to the checkout page after adding a product to cart on my page with product grid:
function bbloomer_redirect_checkout_add_cart( $url ) {
$url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) );
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );
Instead of this I want the current page to reload with a parameter. For example
?addedtocart=productname. Is there any function or hook for it?
EDIT:
When editing the function to this:
function bbloomer_redirect_checkout_add_cart( $url ) {
$url = var_dump($url);
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );
The output is like this:
string(0) "" string(41) "http://URL/winkelmand/"
I think this should work:
function bbloomer_redirect_checkout_add_cart( $url ) {
$param = $_REQUEST['add-to-cart'];
$glue = (strpos($_SERVER['REQUEST_URI'], '?') !== false) ? '&' : '?';
$url = $_SERVER['REQUEST_URI'].$glue.'addedtocart='.$param;
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );
Related
I am trying to use below code that whenever user goes to page name called "Through Phone Call" it should direct him to certain page
In functions.php
function my_custom_add_to_cart_redirect( $url ) {
$page_title = $post->post_title;
if ($page_title == "Through Phone Call") {
$url = get_permalink( 2855 );
} else {
$url = get_permalink( 1853 );
}
}
But the code is not functioning what i am missing?
According to the docs you need to call wp_redirect() like below:
function my_custom_add_to_cart_redirect( $url ) {
$page_title = $post->post_title;
if ($page_title == "Through Phone Call") {
$url = get_permalink( 2855 );
} else {
$url = get_permalink( 1853 );
}
wp_redirect( $url );
exit;
}
I have a wordpress function that adds a query string 'nocfcache=1' to a single page.
function nocfcache_query_string( $url, $id ) {
if( 42 == $id ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Issue: How to use multiple page ids in the function so as to make sure they all have the query string appended.
What I have tried so far:
function nocfcache_query_string( $url, $id ) {
$id = array (399, 523, 400, 634, 636, 638);
if(in_array($post->ID, $id)) {
$url = add_query_arg( 'nocfcache', true, get_permalink( $post->ID ));
return $url;
exit;
}
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Add a custom checkbox field/option for page/post/any (you can use ACF todo it easily).
You can use it to check if page requires to add the query string or not.
function nocfcache_query_string( $url, $id ) {
$custom_checkbox = get_post_meta($id, 'custom_field_name', true); // though for ACF you also use get_field('custom_field_name', $id);
if( $custom_checkbox ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
On certain pages I'm looking to remove the parent page from the URL.
So instead of:
example.com/parent-page/child-page
I'd like it to display:
example.com/child-page
I have tried the following method but no luck:
function wpse_101072_flatten_hierarchies( $post_link, $post ) {
if ( 'page' != $post->post_type )
return $post_link;
$uri = '';
foreach ( $post->ancestors as $parent ) {
$uri = get_post( $parent )->post_name . "/" . $uri;
}
return str_replace( $uri, '', $post_link );
}
add_filter( 'post_type_link', 'wpse_101072_flatten_hierarchies', 10, 2 );
Is there any known solution that works without a plugin?
We use WordPress and would like to link amp to amp if the linked page has an amp version. We have amp structured like that: test.de/test/amp
Unfortunately this code in my functions.php isnt applying to links hard-coded inside of the post content. What do I have to change, so its working for every internal link:
add_filter( 'post_link', function( $url, $post ) {
static $recursing = false;
if ( $recursing ) {
return $url;
}
$recursing = true;
if ( ! function_exists( 'post_supports_amp' ) || ! post_supports_amp( $post ) ) {
return $url;
}
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
$url = amp_get_permalink( $post->ID );
}
$recursing = false;
return $url;
}, 10, 2 );
At the moment its also applying to the canonical link, which is really bad for seo. How to prevent this?
Add these functions to your theme's 'functions.php'.
/* post link filter */
add_filter( 'post_link', 'change_amp_url', 10, 2 );
function change_amp_url( $url, $postobj ) {
static $recursing = false;
if ( $recursing ) {
return $url;
}
$recursing = true;
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
if ( function_exists( 'post_supports_amp' ) && post_supports_amp( $postobj ) ) {
$url = amp_get_permalink( $postobj->ID );
}
}
$recursing = false;
return $url;
}
/* content link filter */
add_filter( 'the_content', 'change_amp_url_content' );
function change_amp_url_content($content)
{
$dom = new DOMDocument();
$dom->loadHTML($content);
$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
$link = $tag->getAttribute('href'); // original url
$extralink = '';
if(stristr($link,'#')) {
$pagelinktemp = explode("#",$link);
$pagelink = $pagelinktemp[0];
$extralink = '#'.$pagelinktemp[1];
} else {
$pagelink = $link;
}
if($pagelink!="") {
$postid = url_to_postid($pagelink);
$postobj = get_post($postid); // getting appropriate post object
if($postobj) {
$newlink = change_amp_url( $pagelink, $postobj ); //new url
}
else {
$newlink = $link;
}
}
else {
$newlink = $link;
}
if($link != $newlink) // change if only links are different
{
$content = str_replace($link, $newlink.$extralink, $content);
}
}
return $content;
}
/* override canonical link */
add_filter( 'wpseo_canonical', 'amp_override_canonical' );
function amp_override_canonical($url) {
if ( substr($url,-4)=="/amp" ) {
$url = substr($url,0,-4);
}
return $url;
}
The first function will provide the AMP URL if exists.
The second one will loop through each URL in the content and change to AMP URL if valid.
The last one will rewrite the canonical URL that displayed via Yoast SEO plugin.
If you want to replace hardcoded links inside of your post content I would suggest you use the "the_content" filter for wordpress.
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
add_filter( 'the_content', 'filter_function_name' )
From this you should be able to regular expression match the link and append /amp to it.
Pseudo code example:
function my_the_content_filter($content)
{
if (function_exists('is_amp_endpoint') && is_amp_endpoint()) {
$patterns = array(
//patterns
);
$replacements = array(
//replacements
);
$content = preg_replace($patterns, $replacements, $content);
}
return $content;
}
add_filter('the_content', 'my_the_content_filter');
I have tested the code submitted by Outsource WordPress, and in general it works fine but the 'amp_override_canonical function overwrites all the urls of the page removing the /amp.
I have made some changes to this piece of code but they do not work as I expect. It seems that the 'wpseo_canonical' function is being invoked in a different context.
add_filter( 'wpseo_canonical', 'amp_override_canonical' );
function amp_override_canonical($url) {
if ( substr($url,-4)=="/amp" ) {
$url = substr($url,0,-4);
}
return $url;
}
I'm having a problem with receiving the value from %category% in $wp_rewrite->page_structure :
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root .'/%category%/%pagename%';
//flush_rewrite_rules();
}
add_action( 'init', 'custom_page_rules',1 );
How can I receive the %category% in a $wp_rewrite->page_structure?
Now it returns:
http://example.com/%category%/the-page-slug/
Instead:
http://example.com/my-cat-slug/the-page-slug/
Note Categories for pages are working well:
http://example.com/my-cat-slug/
returns all pages from this category.
A solution could be:
function rudr_post_permalink( $url, $post ){
if( !is_object( $post ) )
$post = get_post( $post_id );
$replace = $post->post_name;
/* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */
if( $post->ID == 1 )
$replace = 'hello-planet';
if( $post->ID == 12 )
$replace = 'Contacts';
$url = str_replace($post->post_name, $replace, $url );
return $url;
}
add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );
Which is explained here
You can change the if( $post->ID == 1 ) to anything you need.
I have changed the whole code to add categories in the URL's when it are pages.
Regards, Danny