John Davidson

Modify all web URLs with PHP - Wordpress

0 comments
Message:


I want to do a "mass search" of all the URLs on the web to put a parameter at the end of each link.


Example:


1- If there is a string that starts with 'http
2- End with a quote (')
3- Does not end with .mp4, png, jpg
4- Then, add the string . "&mobile" before the quote (').

Do you understand my point? Basically I want to add to all the links the parameter of mobile.


Can you help me? I can't figure out how to do it.
(I'm a newbie to PHP, so I apologize if it's basic)


Update


My code:


if ( isset( $_GET['mobile'] ) ) {
wp_enqueue_style( 'adaptabilidad-nattule', plugins_url( 'css/mobile.css', __FILE__ ), array(), '1.0.0' );


add_filter( 'the_content', 'adaptabilidad_nattule_add_mobile_url' );
function adaptabilidad_nattule_add_mobile_url( $content ) {
$url = get_permalink();
$content .= 'href="' . $url . '?mobile"';
return $content;
}
}

If I enter my website:
mysite.com/?mobile


That function is activated. What I want is to change all the links that appear with the ending of ?mobile



My Answer:
You can modify all web URLs in WordPress using PHP by using the `add_filter` function to modify the `post_link` and `post_type_link` hooks. Here is an example code snippet that you can use in your theme's `functions.php` file:

php
function custom_post_link($permalink, $post) {
// Modify the permalink here
$permalink = str_replace('example.com', 'newexample.com', $permalink);

return $permalink;
}
add_filter('post_link', 'custom_post_link', 10, 2);

function custom_post_type_link($permalink, $post) {
// Modify the permalink here
$permalink = str_replace('example.com', 'newexample.com', $permalink);

return $permalink;
}
add_filter('post_type_link', 'custom_post_type_link', 10, 2);


In this code snippet, we are using the `str_replace` function to replace the old URL (`example.com`) with the new URL (`newexample.com`). You can modify this code to suit your specific needs and replace the URLs as required.

Rate this post

5 of 5 based on 8942 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us