I have a website where I sell digital downloads, and have been trying to set up PayPal express checkout for some time. I am finally getting it integrated, but there is one thing that I can't figure out.
My code says get_script_uri( 'buy.php' ) );
This means that when the payment is processed, the user will be redirected to "buy.php".
PayPal_Digital_Goods_Configuration::return_url( get_script_uri( 'buy.php' ) );
However, my page that includes this code (paypalbuy.php) is in a directory called "dl/". When the payment is processed, and the user is redirected to 'buy.php', the user is redirected to 'dl/buy.php' because of the path.
How can I fix this? How can I set 'get_script_uri( 'http://google.com' ) );' and have the user redirected to a URL, instead of the file within the path.
EDIT: HERE IS my get_script_uri function:
function get_script_uri( $script = 'index.php' ){
// IIS Fix
if( empty( $_SERVER['REQUEST_URI'] ) )
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
// Strip off query string
$url = preg_replace( '/\?.*$/', '', $_SERVER['REQUEST_URI'] );
//$url = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';
$url = 'http://'.$_SERVER['HTTP_HOST'].implode( '/', ( explode( '/', $_SERVER['REQUEST_URI'], -1 ) ) ) . '/';
return $url . $script;
}
Without looking at get_script_uri() source code I can't be sure, but from the directory structure you described, I infer that by changing get_script_uri( 'buy.php' ) to get_script_uri( '../buy.php' ) should give you the correct URL you're looking for: ../ means parent directory.
Related
I am trying to convert category links from queries to Pretty URL's.
For Example, I have category 'ALASKA'.
It's URL is https://www.mywebsite.com/category/alaska/
I am getting some queries from the url using following link:
https://www.mywebsite.com/category/alaska/?subscriber=1
I want to convert this URL to Pretty URL (https://www.mywebsite.com/category/alaska/subscriber/) and also want to display custom template for it.
I tried this code:
Try 1
function my_template() {
if ( is_archive() ) {
if ( isset( $_REQUEST['subscriber'] ) ) {
$myurl = get_category_link();
if (substr($myurl) != "/") $myurl .= "/";
$myurl .= "subscriber/";
$myurl .= trim( sanitize_title_for_query( $_REQUEST['subscriber'] ) ) . "/";
exit( wp_redirect( $myurl ) ) ;
}
}
}
add_action( 'template_redirect', 'my_template_redirect' );
Above code didn't work for me.
I don't have enough knowledge about URL Rewriting in WordPress.
Please help me.
UPDATE:
Try 2:
As mentioned in comment by Oussama
I came up with this code:
function wallpaperinho_subcat_rewrite_rule() {
add_rewrite_rule('^category/([^/]*)/subscriber/?','index.php?page_id=305965&subscriber=1','top');
}
add_action('init', 'wallpaperinho_subcat_rewrite_rule', 10, 0);
The above function works, but I am not able to detect current category ID on the custom page and in the function. Is there any way to do it? So I can display different data with different categories.
url = "https://www.mywebsite.com/category/alaska/?subscriber=1"
pretty = ""
for char in url: ## Loop through every letter in the 'ugly' URL
if char in "abcedfghijklmnopqrstuvwxyz./:": ## If the character is allowed
pretty += char ## Add it to the Pretty URL
EDIT: Just realised that your code is in PHP ... this code is in Python :( ... I'm working on a PHP version.
I am not sure about WordPress, what you are looking for is URL re-writing either with a .htaccess or Apache config.
You can make the link SEO friendly using .htaccess
RewriteRule ^category/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ category/$1/?subscriber=$2 [NC,L]
And then you can retrieve your var subscriber value using $_GET['subscriber']
By default, wordpress creates cookie for logged in users which looks like this:
Cookie Name: wordpress_logged_in_a32e4aa16e20e5346cda1
I checked wordpress core file(wp-includes/default-constants.php) from where this cookie is being created. And how I saw the cookiehash after wordpress_logged_in_ is your site url done with md5().
if ( !defined( 'COOKIEHASH' ) ) {
$siteurl = get_site_option( 'siteurl' );
if ( $siteurl )
define( 'COOKIEHASH', md5( $siteurl ) );
else
define( 'COOKIEHASH', '' );
}
if ( !defined('LOGGED_IN_COOKIE') )
define('LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH);
I need to check whether that cookie exists or has value like below:
if(isset($_COOKIE['wordpress_logged_in_a32e4aa16e20e5346cda1']) && !empty($_COOKIE['wordpress_logged_in_a32e4aa16e20e5346cda1'])){
//do something
}
But now I'm working on test website and the site url is different and once I go live, site url will be changed and cookiehash also will be changed. And I don't want to have issues after going live or after going live copy and paste new hash in my functions.php file.
Is there some dynamic way to check this cookie's existence?
You can get the site URL variable using Wordpress function.
get_site_url();
Example
<?php echo get_site_url(); ?>
//Results in the full site URL being displayed:
http://www.example.com
After getting Site URL. Calculate its MD5 in PHP. Note that to use this function outside wordpress template files, You need include Worpress wp-load.php in your php code with its path.
<?php include '../../../wp-load.php'; ?>
Once MD5 is received you can concatenate string and use in your check. Let me know in comments if you have confusion.
EDIT:
$siteurl = get_site_option( 'siteurl' );
if ( $siteurl ){
$hashedSiteUrl = md5( $siteurl );
$finalUserCookieName = 'wordpress_logged_in_'.$hashedSiteUrl;
}
if(isset($_COOKIE[$finalUserCookieName]) && !empty($_COOKIE[$finalUserCookieName])){
//some action
}
Reference: https://developer.wordpress.org/reference/functions/get_site_url/
The profile-update URLs for User Profiles are language specific: .../nl/profile.php and .../en/profile.php. When users click "Update Profile" I can redirect them to any URL. But now I want to test the current URL to see if there's /nl/ in there, so I can give them a redirect to their own language. I use the code below but the result of the 'if statement' is always false. I can see that: when I enter another url there, it picks up that one. So the code seems to work, just the test fails. Any ideas what I'm doing wrong?
add_action( 'profile_update', 'custom_profile_redirect', 12 );
function custom_profile_redirect() {
if(strpos("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}", '/nl/') === false ) {
wp_redirect( trailingslashit( home_url() . '/en' ) );
} else {
wp_redirect( trailingslashit( home_url() . '/nl' ) );
}
exit;
}
[edit:] Variables I managed to retrieve are all set to 'en' after the profile update, even the plugin's global variable. I now worked around it by adding a LanguagePreference dropdown in the sign up form. In the After-Update-Redirect, I read the usermeta and redirect to their own preference.
There must be better ways to achieve this...
I want to create a route in my wordpress plugin that isn't linked to a page but to an action that sends an email. So i would send a get request like this
example.com/send/email?email=test#test.co.uk
and this would link to an action with the email as a parameter. I'm very new to Wordpress so forgive me if this is a stupid question but I'm really struggling to achieve this or evebn find a good starting point, can anyone help?
A good option in your case would be to use a rewrite endpoint. A rewrite endpoint allows you to add extra query parameters to certain URLs. For instance you can add a gallery endpoint to all posts, that could render a different template showing all images for the given post. More information about add_rewrite_endpoint() can be seen in the Codex.
Below is some code that adds a send endpoint to EP_ROOT(the home page of the site). Note that you'll have to go to Settings > Permalinks after adding this code in order for the rewrite endpoint to start working.
Once we have the rewrite endpoint in place, we hook to the template_redirect action in order to check for the presence of the send query var. If send is not present, then we do nothing.
If send is present, but empty(like for instance if you load the page http://example.com/send/), then we redirect to the home page.
Otherwise we split send into multiple parts at every / and assign that to the $send_parts variable. We then use a switch statement to see what the $send_action(the first part after /send/) and act accordingly.
Right now we're only checking for the email action(if it's not email, we redirect to the home page again). We check if there's an actual email($send_parts[1]) and whether it's a valid email(I have to note that is_email() is not RFC compliant and it might reject valid email addresses, so use with caution). If it's a valid email, we use wp_mail() to send the email, otherwise we redirect to the home page.
Now, since I don't know how you're planning to implement this my code doesn't cover things like authentication(who can send emails - if it's everyone, I can abuse your site and spam users and get your mail server blacklisted - bad :( ), generation of the email Subject and Message(is it going to be dynamic via $_POST variables, is it going to be pre-defined, etc.). Those are specifics that you will have to implement on your own.
Once the code below is placed in an appropriate file(a .php file that gets loaded in the current theme, or an active plugin's file) and you regenerate your Rewrite Rules(by going to Settings > Permalinks), you can go to http://example.com/send/email/your.email#example.com/ and you should receive an email with a subject "Hello" and a message "This is a message".
function so_34002145_add_email_endpoint() {
add_rewrite_endpoint( 'send', EP_ROOT );
}
add_action( 'init', 'so_34002145_add_email_endpoint', 10 );
function so_34002145_handle_send_email() {
$send = get_query_var( 'send', null );
// This is not a /send/ URL
if ( is_null( $send ) ) {
return;
}
// We're missing an action, the URL is /send/
// Take the user to the home page, since this is an incomplete request.
if ( empty( $send ) ) {
wp_redirect( home_url( '/' ) );
exit;
}
$send_parts = explode( '/', $send );
$send_action = $send_parts[0];
switch ( $send_action ) {
case 'email':
$email = ! empty( $send_parts[1] ) ? $send_parts[1] : false;
if ( ! $email || ! is_email( $email ) ) {
// A missing or invalid email address, abort
wp_redirect( home_url( '/' ) );
exit;
}
wp_mail( $email, 'Hello', 'This is a message' );
break;
default:
// This is an unknown action, send to the home page
wp_redirect( home_url( '/' ) );
exit;
break;
}
}
add_action( 'template_redirect', 'so_34002145_handle_send_email', 10 );
I have a page with the the URL format:
www.site.com/events/event.php?id=1&cat_id=4
which I'd like to rewrite to something more SEO friendly, namely
www.site.com/events/name-of-event
(I don't need the category name in my URL, just for the page to create dynamic breadcrumb links).
I have the event name in my MySQL database, and I'd like to use that in the URL. I've tried to adapt the code in this answer but without success. Can anyone help?
EDIT:
RewriteRule ^events/([^/]+)$ event.php?event_name=$1 [L,QSA] this is what I initially tried (the cat_id isn't essential to the URL as the page checks and retrieves a default if none is passed).
For backward compatibility, you probably want to redirect events/event.php?id=123 to the new events/event_name url. The problem is that, as the person said in your linked answer, there is no real way to connect to the database via Apache, so you'll have to handle the redirection/translation in the file itself.
The rule itself must look something like this:
RewriteRule ^events/([^/]+)/?$ /events/event.php?event_name=$1 [L,QSA]
Then, in your event.php you'll do something like this:
#This is the top line of your file.
if( isset( $_GET['id'] ) ) {
$id = intval( $_GET['id'], 10 );
$event_name = do_database_query_NOAW( $id );
header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: /events/' . $event_name );
exit();
}
#code that now uses (the sanitized) event name instead of the sanitized id
if( !isset( $_GET['event_name'] ) || !preg_match( '/[A-Za-z0-9-]+/', $_GET['event_name'] ) ) {
header( 'HTTP/1.1 400 Bad Request' );
exit();
}