Wordpress plugin to store base URL as variable? - php

Is there a plugin that takes any URLs in the post/page content and stores the base url portion as a variable?
For example, store http://www.mydomain.com/about as {$base_url}about
Would be nice for changing URLs around.

If you don't wish to edit your theme, you can use the URL Shortcodes plugin to produce absolute URLs that you can use in your posts and pages.
http://wordpress.org/extend/plugins/url-shortcodes/
Update: If it's to make your WordPress database more portable, you can just use WordPress' native Import/Export functions to bring your content to a new host/domain.
If you need to change all the links in your post content, you can use a plugin like Search and Replace to change the content within your database:
http://wordpress.org/extend/plugins/search-and-replace/

I believe what you're looking for is:
echo get_bloginfo('url').'/about';
Correct me if I'm wrong, but you can get all of the bloginfo from that function.
Source:
http://codex.wordpress.org/Function_Reference/bloginfo

Related

Whats the best way to change the content urls in the WordPress Rest API results?

Say I have api.wordpresssite.com where I will be entering data, uploading images and so on. Then saw that I want to consume the WordPress API on another site like mysite.com.
WordPress will then assume that every link in content will be api.wordpresssite.com It will also embed images with the same URL because the links and media are absolute.
Am I supposed to process the content on mysite.com looking for links but ignoring media or is there a plugin or function that I can add to my theme to do this?
I have tried changing the base URL and while it works for links, it breaks media uploads.
There are multiple endpoints so I am thinking that even if there was a function to add, it would be too far down the line of execution to do anything.
For Example, there is the WP API, as well as JetPack, and Yoast that I am using.
There are tonnes of articles on "How to use WordPress in Laravel" but not a single article has talked about how to "normalize" the content for the site that is consuming it.
The API feels like it's only true out of the box use is to be used with some kind Javascript based front end.
What I would like is a headless WordPress API with relative URLs for content links.
I was looking for the same answer and solved it this way:
What you would need to do is set the site URL to www.remote-domain.com.
You can do this by going to WP Admin > Settings > General
Screenshot
Then on your functions.php file or somewhere you can add a filter (plugin etc.) add this filter in.
add_filter( 'rest_url', 'fix_rest_url');
function fix_rest_url( $url ) {
return $url;
}
Got the answer from: https://core.trac.wordpress.org/ticket/49146
My suspicion that no one really uses the WordPress API seems to be confirmed. Mainly on an external site.
I had to create some render methods that looked for HREF links and remap them on the content, and menus.
It still seems silly and not very polished.

Internal link in WordPress footer.php file not working

I am creating a child theme and I have added a link in the footer.php file to a page within my website. I used the WordPress recommended:
PAGE TITLE
which displays the proper URL when the link is clicked, but I get a 404 error. I determined that if I set my permalink setting to Default it will bring up the page, but the URL is the permalink ID, not the slug. I want to use Post name for my permalinks for SEO purposes. Any suggestions?
With out seeing what the resulting URL is it's hard to say what the problem is. I'm guessing you're using the example here on the get_permalink() function page. My guess is that you simply don't need to use the esc_url() function. According to Stephen Harris get_permalink() performs it's own sanitation so using esc_url() on it is not necessary despite it being "recommended" on the codex.
It would be more helpful if you posted a link to your page or at least the resulting code.

Custom URL rewrite, migrate one CMS to Wordpress

I am trying to move my image gallery site from one CMS to Wordpress.
On current CMS I have page with medium size image on it with URL:
www.my-example-site.com/folder-one/subfolder-one/example-image-name.jpg.html
and another page with full resolution of same image on it with URL like this:
www.my-example-site.com/full-image/folder-one/subfolder-one/example-image-name.jpg-some-custom-keywords.html
I managed to make same address on test WP for medium size image, I created same categories with, and used same URL slug for categories.
After that, under settings --> permalinks I made custom URL structure like:
www.my-example-site.com/%category%/%postname%.jpg.html
and that made me the same urls for page with medium size image on it.
But, problem is with page with full size image on it.
Is there any way (some rewrite rule or something) to get another page to. It could be 301 redirect to something like this:
www.my-example-site.com/folder-one/subfolder-one/example-image-name.jpg.html?-some-custom-keywords.html
or anything similar, just to be able to use data as like on stardard page.
than I can use custom url check (on very top of single.php) to see if page contains "?-some-custom-keywords.html" to include file for generating that new page,
if there is no that variable to include file for generating standard post page.
Please note that there are multiple different combination for "-custom-keywords.html" suffix (about 15)
If you have any question or suggestion please ask.
Regards.
I'm guessing that these URL paths are stored in the database of your old CMS.
If so, you should be changing them there to match the standard URI of WordPress instead of rigging a way for the old paths to work.

WordPress Plugin populating a Page with custom URL parameters

OK, I'm writing a WordPress plugin with a dedicated DB table. I want to display a given record using a WP page. I want to simply include a short code in the page that calls a function to get the url parameters and generate the content. Simple so far. The problem I'm having is I can't find a way use mo_rewrite with WP to have URLS like this:
http://site.com/page/mydbrecordid
I also want http://site.com/page/ to activate the same function obviously with some default output.
I can't find a similar use case documented anywhere.
Thanks!
Perhaps instead of using mod_rewrite to transform your friendly URLs into parameterized ones, you could simply hook the 'template_redirect' action, then render whatever you want according to the path in $_SERVER['REQUEST_URI'] and any other URL parameters.
One thing you could do in the action function is to pull page content from the DB, evaluate it using do_shortcode() and apply the usual filters (wpautop, convertchars, wptexturize). That way, you can register your own shortcodes to pull data from your custom table and use them in any page/post/widget, including pages you render from the template_redirect action hook.
Without knowing all the details of your use-case, it is difficult to know if this is the best option or not.

Create WordPress Page that redirects to another URL

I wanted to create a new WordPress page that is actually a link to another site. The goal is to have the page show up in a list of my pages, but actually send the web user to the target URL.
For example, say I want to include a page that indicates "My Photos" but actually redirects them to Flickr.
I'm guessing one way to accomplish this is by using a custom template page with a redirect instruction in PHP, but unfortunately I am a newbie to PHP and am not familiar with the way to accomplish this...
You can accomplish this two ways, both of which need to be done through editing your template files.
The first one is just to add an html link to your navigation where ever you want it to show up.
The second (and my guess, the one you're looking for) is to create a new page template, which isn't too difficult if you have the ability to create a new .php file in your theme/template directory. Something like the below code should do:
<?php /*
Template Name: Page Redirect
*/
header('Location: http://www.nameofnewsite.com');
exit();
?>
Where the template name is whatever you want to set it too and the url in the header function is the new url you want to direct a user to. After you modify the above code to meet your needs, save it in a php file in your active theme folder to the template name. So, if you leave the name of your template "Page Redirect" name the php file page-redirect.php.
After that's been saved, log into your WordPress backend, and create a new page. You can add a title and content to the body if you'd like, but the important thing to note is that on the right hand side, there should be a drop down option for you to choose which page template to use, with default showing first. In that drop down list, there should be the name of the new template file to use. Select the new template, publish the page, and you should be golden.
Also, you can do this dynamically as well by using the Custom Fields section below the body editor. If you're interested, let me know and I can paste the code for that guy in a new response.
I've found that these problems are often best solved at the server layer. Do you have access to an .htaccess file where you could place a redirect rule? If so:
RedirectPermanent /path/to/page http://uri.com
This redirect will also serve a "301 Moved Permanently" response to indicate that the Flickr page (for example) is the permanent URI for the old page.
If this is not possible, you can create a custom page template for each page in question, and add the following PHP code to the top of the page template (actually, this is all you need in the template:
header('Location: http://uri.com, true, 301');
More information about PHP headers.
Alternately, use a filter.
Create an empty page in your WordPress blog, named appropriately to what you need it to be. Take note of the post_id. Then create a filter that alters its permalink.
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
}
return $permalink;
}
This way the url will show up correctly in the page no funny redirects are required.
If you need to do this a lot, then think about using the custom postmeta fields to define a postmeta value for "offsite_url" or something like that, then you can create pages as needed, enter the "offsite_url" value and then use a filter like the one above to instead of checking the post_id you check to see if it has the postmeta required and alter the permalink as needed.
I'm not familiar with Wordpress templates, but I'm assuming that headers are sent to the browser by WP before your template is even loaded. Because of that, the common redirection method of:
header("Location: new_url");
won't work. Unless there's a way to force sending headers through a template before WP does anything, you'll need to use some Javascript like so:
<script language="javascript" type="text/javascript">
document.location = "new_url";
</script>
Put that in the section and it'll be run when the page loads. This method won't be instant, and it also won't work for people with Javascript disabled.
Use the "raw" plugin https://wordpress.org/plugins/raw-html/
Then it's as simple as:
[raw]
<script>
window.location = "http://www.site.com/new_location";
</script>
[/raw]
There are 3 ways of doing this:
By changing your 404.php code.
By using wordpress plugins.
By editing your .htaccess file.
Complete tutorial given at http://bornvirtual.com/wordpress/redirect-404-error-in-wordpress/906/
I found a plugin that helped me do this within seconds without editing code:
https://wordpress.org/plugins/quick-pagepost-redirect-plugin/
I found it here: http://premium.wpmudev.org/blog/wordpress-link-title-external-url/
There is a much simpler way in wordpress to create a redirection by using wordpress plugins. So here i found a better way through the plugin Redirection and also you can find other as well on this site Create Url redirect in wordpress through Plugin
(This is for posts, not pages - the principle is same. The permalink hook is different by exact use case)
I just had the same issue and created a more convenient way to do that - where you don't have to re-edit your functions.php all the time, or fiddle around with your server settings on each addition (I do not like both).
TLTR
You can add a filter on the actual WP permalink function you need (for me it was post_link, because I needed that page alias in an archive/category list), and dynamically read the referenced ID from the alias post itself.
This is ok, because the post is an alias, so you won't need the content anyways.
First step is to open the alias post and put the ID of the referenced post as content
(and nothing else):
Next, open your functions.php and add:
function prefix_filter_post_permalink($url, $post) {
// if the content of the post to get the permalink for is just a number...
if (is_numeric($post->post_content)) {
// instead, return the permalink for the post that has this ID
return get_the_permalink((int)$post->post_content);
}
return $url;
}
add_filter('post_link', 'prefix_filter_post_permalink', 10, 2 );
That's it
Now, each time you need to create an alias post, just put the ID of the referenced post as the content, and you're done.
This will just change the permalink. Title, excerpt and so on will be shown as-is, which is usually desired. More tweaking to your needs is on you, also, the "is it a number" part in the PHP code is far from ideal, but like this for making the point readable.

Categories