Wordpress - how to change absolute links to relative links - php

I've just completed my first Wordpress site for a client and sent the files via FTP transfer to their server. I've just received the following comment back -
"...a few things don’t seem to be displaying properly, or linking correctly… It looks as though they’ve used absolute links instead of relative ones, so certain things aren’t pulling through properly..."
I didn't even realise this would be an issue as I assumed all the links would require changing anyway. Is there a code function that can go in the functions.php file to amend this? I've seen that there is a wp_make_link_relative and the following filters -
add_filter( 'post_link', 'wp_make_link_relative' ); // Normal post link
add_filter( 'post_type_link', 'wp_make_link_relative' ); // Custom post type link
add_filter( 'page_link', 'wp_make_link_relative' ); // Page link
add_filter( 'attachment_link', 'wp_make_link_relative' ); // Attachment link
add_filter( 'get_shortlink', 'wp_make_link_relative' ); // Shortlink
Should I apply these to my functions.php file? Will that fix everything or do I need to apply anything else, like a plugin?

To achieve relative links for your images/icons, JS and CSS assets do the following.
I suppose you've your assets layed out like this,
- theme-name
- - assets
- - - images
- - - css
- - - js
So, to access a file named scripts.js in your js directory, use the following code to link to it
get_stylesheet_directory_uri() . '/assets/js/scripts.js'
It'll return the following URL
http://example.com/wp-content/themes/theme-name/assets/js/scripts.js

I don't know if it's the best solution, but it works.
Add this constant in index.php
define( 'WP_CONTENT_URL', '/wp-content');
Topic about this solution is there: Relative URLs in WordPress

There are several places where URLs get defined. The above lines should work for most of them, but not media or any custom API work. Also, WordPress SEO plugins like Yoast create absolute URLs, which can negatively impact your SEO if you edit them in the database.
If they say "things don't seem to be displaying properly..." they may mean assets. For that, go to
Settings > Media > Full URL path to files and change it to
/wp-content/uploads.
For absolute URLs in JS files, in functions.php create a function like this:
function create_root_url() {
wp_localize_script( '<your-main-js-bundle>', 'any-variable-name', array(
'root_url' => get_site_url()
));
}
add_action( 'wp_enqueue_scripts', 'create_root_url' );
Then in your JS file, change your hard-coded url to:
const endpoint = `${any-variable-name.root_url}/wp-json/api...`;
or whatever your purpose may be.

Related

Adding .php extension to one plugin page in Wordpress

I'm searching for a solution to add .php extension to the page of my wordpress plugin.
I already found similar posts but none of them was about adding the .php extension to only on page generated by a plugin.
I already tried to work with the global $wp_rewrite; but it would apply the .php extension to all pages.
All I want is simply something like that:
www.mydomain.com/myfile => www.mydomain.com/myfile.php but only for this one particular page.
Update:
The file myfile.php doesn't exist. It's the permalink of a wp page. I basically want to add .php to the permalink of one page (only this one page). I know it's possible to change the permalink of an page, but wp would not let me add .php to the permalink. It automatically changes it to -php (it doesn't accept the dot).
It is not necessary to change the link format for all pages via wp_rewrite. You can add rewrite_rule for a specific page without changing the format for the entire Post Type.
add_rewrite_rule(
'^myfile.php',
'index.php?page_id=1234',
'top'
);
1234 = your page $post_id
Next, we change permalink generation for our page
add_filter( 'page_link', 'filter_function_myfile_link', 10, 3 );
function filter_function_myfile_link( $link, $post_id, $sample ){
if ( $post_id == 1234 ) {
$link = home_url('/myfile.php', 'https');
}
return $link;
}

Styles not working in wordpress plugin | wp_enqueue_styles()

I have been searching the web left right and center for a solution to get the wp_enqueue_style() function to work but I just can't get it.
Code Snippet
//Add some styles to the script
function sreub_enqueue_styles() {
//Use it!
wp_enqueue_style ( 'sreubmainstyle', plugin_dir_url(__FILE__) . 'sreubmainstyle.css' );
}
add_action( 'wp_enqueue_scripts', 'sreub_enqueue_styles' );
I have echoed the path I am using in the wp_enqueue_style function and it is correct but have no idea why the styles are not being applied when I put them in the CSS file?
Switch theme and try. or deactivate the plugin and active again. it should works. This code work for me.
function sreub_enqueue_styles() {
wp_enqueue_style( 'sreubmainstyle', plugin_dir_url( __FILE__ ). 'sreubmainstyle.css' );
}
add_action( 'wp_enqueue_scripts', 'sreub_enqueue_styles' );
Let's do a couple of checks first to determine where the root cause lies:
Check the path
Let's check the path specified to the enqueue instruction. The path you have says the CSS file is located in the same folder (and not a subfolder) as the PHP file that has the enqueue callback.
Is this the case?
Check to see where the CSS file is located in relationship to the PHP file. If it's in a subfolder or elsewhere, you will need to modify your pathing.
For example, let's say the CSS file is located in wp-content/your-plugin/assets/css/sreubmainstyle.css but the PHP file which enqueues it is located in say wp-content/your-plugin/load-assets.php, the URL path to the CSS file would need to absolute by including the full path.
Just double check. If the path is correct, then let's go to the next check.
Loading Before the theme's CSS
The theme's loads after the plugin. WordPress loads plugins first and then the theme. Your code is pre-registering the callback with a priority of 10. More than likely the theme is also using the default of 10. That means the plugin's CSS will load first into the <head>.
Check the <head> and see if where the stylesheet is loaded in relationship to the theme. Use Firefox or Chrome Dev Tools to inspect the HTML markup.
If you find it's not loaded into the DOM (meaning in the HTML markup, then we are back to an enqueue problem. If yes, then go to the next check.
Else, I suspect it's there but before the theme's ’style.css` file. If yes, continue reading this section.
You want your styles to come after the theme to ensure yours override the theme and take a higher precedence. In this case, you want to change the priority to something greater than the default of 10. To do this:
add_action( 'wp_enqueue_scripts', 'sreub_enqueue_styles', 50);
The 50 sets the priority higher, meaning it fires later.
Whoops, CSS is not in DOM
Looking in the HTML markup using Firefox or Chrome, you discovered that it did not even load. It's not there. Okay, you know that the path is right. Next did you load the file that is doing the enqueuing? And did you load it before WordPress fires the event to enqueue?
Check when you are loading the file.
try to run add_action( 'wp_enqueue_scripts', 'sreub_enqueue_styles' ); from the main file or make sure add_action( 'wp_enqueue_scripts', 'sreub_enqueue_styles' ); is run on the activation hook callback

Wordpress conditional permalink

I have a specific page on my wordpress website example.com/download/ that I already have a page for. I would like to add some sort of php code to enable the following functionality.
If there is a subpath after the /download/<name>, I would like to fetch a file from some directory based on <name>
If no subpath is specified (/download/) I want to display the page that is already written
Is this possible with Wordpress hooks/filters?
I have a feeling it has something to do with the WP_Rewrite class, but I am not sure exactly what to write, nor where to put it without WP or theme updates wiping the code
Yes, you have to add a custom rewrite rule. You can use your theme's functions.php to add the new functionality.
function custom_rewrite_rule() {
add_rewrite_rule('^download\/([^/]+)\/?','your_url_to_the_downloader_file.php?download=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
To make it work, first you need to go to Admin -> Settings -> Permalinks and click the save button so the new rule is added.

How to hide some parts of URL part for custom page in wordpress

I am new in WordPress. I have created two custom pages in my wordpress plugin. When I am redirecting to those pages than URL is showing like this:
http://myDomainName.com/wp-content/plugins/camophoto/php-sdk/wp_mypage.php
and I want to show it like this:
http://myDomainName.com/wp_mypage.php
How can I hide the part???
wp-content/plugins/camophoto
I have a solution but not know how and where to use it??
add_action( 'init', 'add_author_rules' );
function add_author_rules() {
add_rewrite_rule(
"camoPhoto.php/?",
"wp-content/plugins/camophoto/php-sdk/camoPhoto.php",
"top");
}
Please any body help me.
The easy way would be to install the plugin "WP No Category Base". If you dont want to do this, than there is another possibility.
If you have access to the .htaccess file you can change the direction.
First backup your .htaccess file. Now open and append the following line:
RewriteRule ^wp-content/plugins/camophoto/(.+)$ http://myDomainName.com/$1 [R=301,L]

Where is the best spot to place extra files on a Wordpress install?

I am making changes to an existing website that is based on WordPress (some javascript add-ins and a few PHP scripts for Ajax purposes).
Is their any proper directory that I should place all these files? I started off putting them in a folder in the root directory called assets, but then decided maybe they should go with the rest of the Wordpress template and javascripts files? Or should I keep them out of the wp- directories, and simply keep them in the assets folder?
I know its a trivial question, but I like doing things right- having them in directories that make sense.
You can keep all your javascript in a folder in your theme's directory, that is wp-content/themename/.
Concerning Ajax, it's implementation is different in WordPress. You must add a data variable called action in your ajax request and then hook it to a function in your functions.php file. Your ajax url should be wp-admin/admin-ajax.php available through admin_url( 'admin-ajax.php' ) in PHP.
Read AJAX in Plugins for examples.
If it is presentation related I believe you should place it in the corresponding theme folder. If it is a functionality you might want consider wrapping it inside a plugin and placing it in a plugin folder.
If you dont care about abstracting this change and making available to other themes, then I would just simply add it inside the theme folder. You have WP helper functions to get the web path to the current theme folder, or to include js from that folder.
The proper place in WordPress ecosystem is the folder wp-content, as this is the one that you preserve while doing WP upgrades, restorations or migrations.
In that folder, it could be part of a theme (/themes), a plugin (/plugins), an uploaded file (/uploads) or, if the situation requires, a custom folder (/my-custom-content).
/wp-content/themes/your-theme
Here, all presentation related code. It's a common mistake to place general functionality code in the theme's functions.php. The first question to be asked before placing custom functions in this file is:
If I change my theme, will I need this function?
See: Where to put my code: plugin or functions.php?
/wp-content/plugins/your-plugin
Let's say you need to enqueue some CSS or Javascript files, and this should happen whatever theme is being used.
The following sample plugin will load SWFObject (bundled with WP) in the page Map and the front page, as both contain a SWF Flash embed. And, in the rest of the site, it will load the WebFont Loader from Google CDN and a CSS file from within the plugin folder.
<?php
/** Plugin Name: My Enqueue Plugin */
add_action( 'wp_enqueue_scripts', 'enqueue_so_16354990' );
function enqueue_so_16354990() {
global $post;
if( $post->post_name == 'map' || is_front_page() ) {
// Enqueue bundled script
wp_enqueue_script( 'swfobject' );
}
else {
// Enqueue from external location
wp_enqueue_script(
'my-web-font',
'//ajax.googleapis.com/ajax/libs/webfont/1.4.2/webfont.js',
array( 'jquery' ), // Dependencies
time(), // Version, use time to prevent caching
true // Enqueue on footer
);
// Enqueue from within plugin directory
wp_enqueue_style( 'my-css', plugins_url( 'css/my-plugin.css', __FILE__) );
}
}
/wp-content/uploads
Here, your theme or plugin should place all user uploaded content, so it can be managed through WP Media Library screen. And the content should survive any theme swap or plugin de-activation.
/wp-content/custom-folder
Many image gallery plugins use this approach to store their custom media library.
Another use is for Mobile themes, where custom user themes are placed in this folder, so not to lose it on plugin update (as everything in the plugin or theme folders is replaced on upgrades).

Categories