Add custom URL segment to wordpress page - php

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

Related

Add custom htaccess record for custom url path rewrite within wordpress

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.

using wp_redirect to redirect wordpress pages

Since I don't want to use another plugin to do simple redirect tasks, I decided to use the following code.
wp_redirect( "http://www.example.com/contact-us", 301 );
so here is my question.
Let's say I have a page called "https://www.example.com/contact-us-3/" that needs to redirect to "https://www.example.com/contact-us/".
Or I should say I really want the redirect to happen regardless of http or https, www or non-www.
I am want "/contact-us-3/" to redirect to "/contact-us/" page.
Does that mean I have to put the following code inside the wordpress contents? Where do I exactly put the code? function.php in the child theme? I do I specify the page that needs to be redirected? or I do have to create a page "/contact-us-3/" and put the code in the page?
Also, do I have to put the fully qualified domain name url?
You may want to put your redirect code into a callback function that is tied to the template_redirect hook. Put code similar to the following in the functions.php file of your theme. The function named "some_condition" (which you write) will determine if the page should be redirected or not.
add_action( 'template_redirect', 'my_callback' );
function my_callback() {
if ( some_condition() ) {
wp_redirect( "http://www.example.com/contact-us", 301 );
exit();
}
}

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.

Wordpress Alter a Theme Admin Page

I am really a Drupal developer but got asked to do a bit of Wordpress work and need a little help.
I am running WP3.8.x
We are using a purchased Auction Theme and I wish to extend it's functionality by adding another payment gateway.
Here is what I have:
The Auction theme registers a new admin menu
add_action('admin_menu', 'AuctionTheme_admin_main_menu_scr');
Then registers the sub page
function AuctionTheme_admin_main_menu_scr()
{
add_submenu_page(
"AT_menu_",
__('Payment Gateways','AuctionTheme'),
AuctionTheme_disp_spcl_cst_pic('gateway_icon.png') . __('Payment Gateways','AuctionTheme'), $capability, 'AT_pay_gate_', 'AuctionTheme_payment_gateways'
);
do_action('AuctionTheme_new_page_admin_menu');
}
And finally the payment config page is created.
function AuctionTheme_payment_gateways()
{
echo 'The Markup';
do_action('AuctionTheme_payment_methods_action');
if(isset($_POST['AuctionTheme_save1'])) {
update_option('AuctionTheme_paypal_enable', trim($_POST['AuctionTheme_paypal_enable']));
}
}
Obviously I removed some code as it's not relevant to my question.
So we have our own child theme called mytheme. I thought I could just add a new action?
So in mytheme function file added
add_action('AuctionTheme_payment_gateways', 'mytheme_payment_gateways');
function mytheme_payment_gateways () {
echo 'Test';
}
I don't see test in the page …. should? Or how do I go about adding?
In order for your add_action to work, you must have a do_action that calls the same tag.
So, for example, you have:
add_action('AuctionTheme_payment_gateways', 'mytheme_payment_gateways');
Which means that somewhere in your code (parent or child theme) you must have the following:
do_action('AuctionTheme_payment_gateways');
So, based on the code you have, I suspect you want to add it here:
function AuctionTheme_payment_gateways()
{
echo 'The Markup';
do_action('AuctionTheme_payment_methods_action');
if(isset($_POST['AuctionTheme_save1'])) {
update_option('AuctionTheme_paypal_enable', trim($_POST['AuctionTheme_paypal_enable']));
}
// Add below do_action
do_action('AuctionTheme_payment_gateways');
}
Of course that's just a guess based on your code - you may need to add the do_action somewhere else, but hopefully this explanation gets you pointed in the right direction.

404 Redirect not working

Hi I'm type to make a plugin which shows a page instead of sending a 404 page when on a page that doesn't exist
I know I can edit to 404.php file to do this but i need to fit this in a plugin.
If I remove the if statement then its works on all pages which isn't what I want.
So i put the if(is_404()) statement in but it doesn't seem to work at all now...
function da_404_redirect()
{
if(is_404())
{
query_posts('page_id=2');
status_header( "200" );
}
}
add_action('init','da_404_redirect');
Your action hook may be too soon in the wordpress cycle to know if it is a 404. http://codex.wordpress.org/Plugin_API/Action_Reference/init. Try using a later action hook.
As Nick says above you might be hooking into a late-firing event. In my experience, template_redirect is the best option.
To redirect, don't hook on init, especially if you use conditional tag like is_404(). Any conditional tag will still return false in init.
USE: template_redirect instead or pre_get_post if you need to change something on the WP_Query

Categories