Wordpress conditional permalink - php

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.

Related

How to add(include) elementor created page in theme file 404.php

I created a page using elementor(named as: 'Error-page'), currently using linoor theme. What I want is to load this 'Error-page' via theme's default 404.php file (NOT using any plugin). Should I just type in 'include Error-page.php' in 404.php file to make it work?
Meta: My goal is to show my custom error page which I created using elementor(named as: Error-page) when invalid url is typed. One way of doing it is using plugin(I prefer not to do this for the time being), 2nd way could be somehow including this custom page in theme's default 404.php file
Note: Im new to wordpress
after thinking about your question, i have 2 solutions for you:
1. if you do not have code knowledge
This way is simple and safer, but have limits than other way - it's not using your template but redirect viewer to your template page. Any way, the same result
Build the 404 template with Elementor and publish it
Open your theme's 404.php file
In the very first top of the template file **(below <?php tag) paste this code:
wp_redirect( '/your-template-slug' );
exit;
Save it and you're done
2. If you have basic code knowledge:
This way is a litle complex, but it alow you to using not for 404 file - but other templates (archive, categories...)
Install "Anywhere Elementor" plugin
Build the 404 template as you do with Elementor
Get your template php shortcode
Edit the theme's 404.php file to echo out your template
In case you need more detail guide, i have record a step by step guide video here

Wordpress dynamic child page without 404

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.

Can't find where the do_action("This function") leads to in wordpress theme

I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.

Add custom URL segment to wordpress page

I'm trying to allow my page to receive query variable but to rewrite it to nice permalink. So I wanted this
example.com/wordpress-page/random
So I don't want random to be a subpage or something like that, since it's coming from outside service. In order to do this, I've added this code to my functions.php
function add_my_var($public_query_vars) {
$public_query_vars[] = 'my_var';
return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');
function do_rewrite() {
add_rewrite_rule('wordpress-page/([^/]+)/?$', 'index.php?name=wordpress-page&my_var=$matches[1]','top');
}
add_action('init', 'do_rewrite');
When I go to example.com/wordpress-page I get my page, but when I go to example.com/wordpress-page/random I get 404 page.
I have flushed rewrite rules by saving permalinks in wp-admin panel.
Where did I go wrong?
I found I needed to call the flush_rewrite_rules() function. That was done in addition to the query_vars action hook and add_rewrite_rule() function you already demonstrated above.
In my case I chose to put it in my theme's functions.php using the switch_theme action hook and deactivated/reactivated my theme. But you can probably put it elsewhere as long as it get's called once at some point (so probably not 'init')
add_action('switch_theme', 'mytheme_setup_options');
function mytheme_setup_options () {
flush_rewrite_rules();
}

Wordpress - How to make plugin's short code usable in text widget

i've written a plugin which shortcodes can easily be used in every post and page. As this plugin can be useful in a sidebar as well i want to make the text widget usable for my shortcodes.
When i googled this i found out that i can use the add_filter() function to ensure that, but this is only possible if i have access to the theme's functions.php. But as i am the creator of the plugin and not of the theme, this is not usable for me.
Does anybody know how i can make a shortcode which is introduced with a plugin usable in the widgets section?
Thanks!
Open your theme's function file.
Find a free spot after the opening php tag that isn't part of a function.
add this:
if (!is_admin())
{
add_filter('widget_text', 'do_shortcode', 11);
}
save the file and you should be all set.
Open your page in edit mode.
Select your page location and line where you want to add short code.
Add code here and update..

Categories