Adding query string to multiple page urls in a Wordpress function - php

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 );

Related

Using gform_after_submission with gform_notification

I am trying to use gform_after_submission to grab a field value(url of a file) then pass the value to a notification so it can be sent as an attachment. This is what I have so far:
add_action("gform_after_submission", "after_submission", 10, 2);
function after_submission($entry, $form){
$pdf = $entry["3"];
return $pdf;
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = 'path/to/file.pdf';
$notification['attachments'] = array( $path );
}
return $notification;
}
Untested but you shouldn't need the first action, only the notification filter.
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$notification['attachments'] = array( $path );
}
return $notification;
}
Thanks Dave, couldn't see your updated code but here's what I managed to do once you told me about converting the URL to a path & it now works perfectly:
function get_file_path_from_url( $file_url ){
return realpath($_SERVER['DOCUMENT_ROOT'] . parse_url( $file_url, PHP_URL_PATH ));
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$path = get_file_path_from_url($path);
$notification['attachments'] = array( $path );
}
return $notification;
}

Refresh current page with parameter after adding to cart WooCommerce

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' );

Change status of woocommerce products by php function

I have a function which help me to make a redirect of a product if that product doesn't exist anymore in my affiliate xml.
What I want now is to put the product on draft but to redirect his link to a "This products doesnt exist anymore page" - for seo purposes
I didn't try something because I dont know what.
I saw something here and I think my answear is here but i don't know how to apply it:
Change product status if prices are updated in Woocommerce 3
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I expect that my code to remove the product from my website feed and redirect his link to a page of my website.
For seo purposes I cannot delete the product for all I just need to hide it from my shop but keep the link for google (also the pictures).
You can use WordPress function to update the status of the product where your logic of product doesn't exist anymore in my affiliate xml. exists.
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
/* Start Here*/
$my_product = array(
'ID' => $import->id,
'post_status' => 'draft',
);
wp_update_post( $my_product );
/* End Here*/
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );

Wordpress Category for pages

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

Run function on posts sequentially

I have the following (resource intensive) function that I am triggering on the save_post action. It works perfectly when I save OR edit a single post. The problem is that I need to use it when importing multiple posts at the same time. I believe it is running this function across ALL imported (saved) posts at the same time.
How can I use the save_post action BUT only have this function execute on each post sequentially?
function getYouTubeTags( $post_id ) {
// Terminate if post has any tags
if ( has_term('', 'post_tag') ) {
return;
}
$out = null;
$video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
$tag_url = "http://www.youtube.com/watch?v=" . $video_id;
$sites_html = file_get_contents($tag_url, null, null, 700, 7000);
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_tag = null;
foreach( $html->getElementsByTagName('meta') as $meta ) {
if( $meta->getAttribute('property')==='og:video:tag' ){
$meta_og_tag = $meta->getAttribute('content');
$out[] = $meta_og_tag;
}
}
if ( !empty($out) ) {
return implode(',', $out);
} else {
return;
}
}
// Add YouTube tags to post
function rct_save_post_terms( $post_id ) {
$terms = getYouTubeTags( $post_id );
wp_set_post_terms( $post_id, $terms, 'post_tag', true );
}
add_action( 'save_post', 'rct_save_post_terms', 110, 1 );

Categories