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;
}
Related
I want to create a subpage, e.g. /test/ and everything that I enter after /test/ should have one specific template and indexing, no 404 error.
I wanted to make this with virtual pages, but it's too many url's to add. (tried here - Wordpress fake/virtual folder)
I've got my template page-pagename.php which works. Now I need to add that every child of test does not return 404.
I think I have already searched the entire internet and cannot find a solution to this task
What you could fiddle with is with grabbing the main query on 'template_include'. You add an action like this. You can add it to your child theme in functions or in your custom plugin.
add_action( 'template_include', 'custom_router' );
Then in the custom router function, you can check the parameters that you are requesting ('test') and redirect to a template of your choice. Also add in functions.php or in custom plugin. Place a template file in the relevant path.
function custom_router( $query ) {
global $wp_query;
if($wp_query->query['name'] == 'test') :
var_dump($wp_query->query['name']);
var_dump($wp_query->query['page']);
return dirname( __FILE__ ) . '/some-template.php';
endif;
}
I tested the code in the latest wordpress version with a custom plugin and the default theme btw.
I've integrated a custom PHP script within my wordpress instalation (with theme page-template).
The page is /islands/island/ and I get the islandId with GET paramater. So the URL looks like https://website.com/islands/island/?iid=1
But now I want to rewrite the url to be like this: https://website.com/islands/island/1
I already tried edditing the .htaccess but with no luck.
After some research I found editing .htaccess is not the right way to do this. So I used the followingn code and added it to my theme's function.php.
function add_directory_rewrite() {
add_rewrite_tag("%iid%", '([^/]*)');
add_rewrite_rule('^islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );
But unfortunately it is not working. When I browse to the page http://website.com/islands/island/1 it redirects to http://website.com/islands/island/.
Am I missing something?
Got it working!
Working code:
function add_directory_rewrite() {
add_rewrite_tag("%iid%", '[\d+]');
add_rewrite_rule('islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );
Changed the placeholder tags as #Mario suggested.
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.
I am appending "?sel" to my URL in order to keep track of which link was selected, and then show items based on that selection in the next page. I used the following in functions.php to register it as a Query var (rather than just use $_GET):
function add_query_vars_filter( $vars ){
$vars[] = "sel";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
Then I created the link with the var using add_query_arg() in the template where the links are.
$link = add_query_arg( 'sel', $slug, get_the_permalink($news_specials_listing_page_id) );
<a href="<?php echo $link ?>">
However, if I click that link, I still see the "?sel" in the URL. I've got my permalinks setting to post name in settings > permalinks, so I don't see the typical WordPress variables. Is it posible to hide "sel" along with the rest of the registered query variables, and still grab the value using get_query_var()?
I have also tried adding a custom rewrite tag and flushing my permalinks.
function custom_rewrite_tag() {
add_rewrite_tag('%sel%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
I have been experimenting with add_rewrite_rule() and have found that it adds RewriteRules to .htaccess. So if I add a rewrite rule that captures the sel variable in a regular expression and redirect it, would the variable still get saved? I'm also having a difficult time figuring out what the rule would be.
The whole point of using GET is to put variables into your URL. This forms a unique URL that can be bookmarked and indexed by search engines. If you do not want to do this use POST instead. POST allows you to encrypt variable names making it far more secure than the solution you are asking about.
I have a Wordpress website that was about 5-6 custom post types. I have an archive.php and all the rest needed files. Most of them render good with my archive page but 2-3 render with my index.php.
What I tried:
I tried to make a page for each custom post, archive-{post-type} etc.. but that didn't work.
-
Does anyone have an idea about what might cause some of them to render good and only 2-3 to be problematic?
Go through the below following tips and snippet.
Remove if spaces on the top of your custom post archive file.
Go to Settings > Permalinks, change it to something else temporarily, press save, change it back to the original setting, press save again.
The below code snippet in your theme function.php
function get_custom_post_type_template($template) {
global $post;
if ($post->post_type == 'my_custom_post_type') {
$template = dirname( __FILE__ ) . '/archive-my_custom_post_type.php';
}
return $template;
}
//add_filter( "single_template", "get_custom_post_type_template" );//for single page
add_filter( "archive_template", "get_custom_post_type_template" ); //for archive