I'm using a setting from my plugin to generate the rewrite rule:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','/'.$slug.'/?player=$matches[1]','top');
I am getting the correct slug.
I have a url that looks like this:
mysite.com/info/?player=joe.smith.01
I want it to look like this:
mysiste.com/info/joe.smith.01
I'm using the add_rewrite_tag so WP will grab the variable for the url. When I visit a page url structured like what I'm trying to achieve I get a page not found.
I would not recommend modifying the htaccess file if you don't really have to. WP's Rewrite APi should do just fine.
That said, I'm pretty sure you have to go via 'index.php' in the rewrite argument of add_rewrite_rule().
I don't have any way of testing this right now, but something along the lines of the following might work:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
$page_id = $post->ID; //Get the post ID to include in the page_id query var
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','index.php?page_id='.$page_id'.&player=$matches[1]','top');
Related
This is a question for Wordpress experts:
Where, in the Wordpress machine, is the code that decides whether to route a query to the standard page or to a category? Does it try to match specific strings in the URL looking for categories and then, if it doesn't get a match, default to the code that looks for the page in the wp_posts table?
This is something I have been wondering about for a while but I have found it difficult trying to trace the path of the query through the system.
Thanks for any insights you can give!
There is a function for this you can use to hook in a page or a category.
function is_category( $category = '' ) {
}
function is_page( $page = '' ) {
}
$category / $page can be ID, name, slug or array
Is there a way to create an alternate permalink for a specific content type in Wordpress?
For example, I want an alias url:
https://example.com/123
to point to an existing permalink such as:
https://example.com/something/this-is-a-post
I don't mind writing code if I have to.
After a little playing around, I came up with a method. First I set up an action on the init hook. Then I use the following code to confirm the id from the URL is valid and perform a redirection to the permalink.
function my_init() {
global $wpdb;
// Using an alias id to redirect to permalink
// split URL path into parts and look at the first part (index 0)
$path_parts = explode("/", substr(wp_parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), 1));
// make sure we're dealing with an number
if (is_numeric($path_parts[0]))
{
// query the database using the numeric value to see if it corresponds to a post ID of the mp type and its status is publish
$results = $wpdb->get_results("SELECT post_type, post_status FROM wp_posts WHERE ID = $path_parts[0]");
if ($results[0]->post_type == 'mp' && ($results[0]->post_status == 'publish' || is_admin()))
{
// redirect to permalink url
wp_redirect(get_post_permalink($path_parts[0]));
exit;
}
}
}
The default link for pagination looks like "site.com/articles/page/2", but I need to do the page number as parameter(example: "site.com/articles?page=2"). I've tried to use add_filter function:
add_filter('get_pagenum_link', 'edit_paginate_url');
function edit_paginate_url($url){
$pagenum = preg_match("/\/\d\//", $url, $pagenum);
$pagenum = preg_replace("/\//", "", $pagenum);
$url = preg_replace("/\/page\/\d/", "?page=", $url);
return $url . $pagenum;
}
But it didn't work. It returns url like: "site.com/articles0/page/2/" (this filter only adds zero symbol before pagination part of url).
Follow the below url in which you have to set base and format as per your requirements.
https://codex.wordpress.org/Function_Reference/paginate_links
wp_get_attachment_url() process full file path like
http://example.com/wp-content/uploads/2014/12/aura.mp3
I want the url without http://example.com/
So, I want above example as wp-content/uploads/2014/12/aura.mp3 instead of http://example.com/wp-content/uploads/2014/12/aura.mp3. How to do it?
You can really easily explode it by / and then take the part with index 3. Example
$url = wp_get_attachment_url(id); //id is file's id
$urllocal = explode(site_url(), $url)[1]; //output local path
Here is the WordPress way using WordPress functions (avoid hacking):
$fullsize_path = get_attached_file( $attachment_id ); // Full path
$filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
WordPress has tons of functions, so first try to find the function on the docs: https://developer.wordpress.org/reference/functions/get_attached_file/
You can use PHP's function explode.
Here is the code:
<?php
$image_url = wp_get_attachment_url( 9 ); //ID of your attachment
$my_image_url = explode('/',$image_url,4);
echo $my_image_url[3];
?>
You can implode your entire url on / and array_slice from the end, then implode it back in on /.
$url = wp_get_attachment_url($item->ID); //id is file's id
$url_relative = implode(array_slice(explode('/', $url),-3,3),'/');
//Returns: 2019/08/image.jpg
That way if your WordPress is on a subdomain or localhost or the images are on S3 it won't crash.
i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();