Internal link in WordPress footer.php file not working - php

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.

Related

Wordpress: changing only one page url from database

In Wordpress I created a page "home" (https://sitedomain.com/home) but I cannot render it equal as the homepage (I made a page template equal to index.php and implemented the custom query but still not working properly).
I currently have a
echo("<script> window.location.replace('https://sitedomain.com')</script>"); statement in the single page template file allowing me to redirect users to the actual homepage but it is slow. I want to manually change the URL of this page in the database but I cannot find it in the Database.
Also, it is not possible to modify the URL from the dashboard since the minimum url must be https://sitedomain.com/home.
Is it possible? Where to find the single URL of the page in the Database?
You can just define that page as your homepage in the WP dashboard: "Settings > Read > select static page as starting page" (or similar, not sure about the exact English terms)
in the end I simply used JQuery to replace the href attribute with the one that I needed (https://sitedomain.com).
Not ideal solution but it works.

How do I prevent a dynamic link change in PHP?

I develop in ASP.NET MVC so please forgive my lack of the basics when it comes to PHP and WordPress. We have a WordPress site where the terms and conditions link in the footer is broke on one page but works on another.
On our home page (http://www.ourdomain.com) there is a link and when I hover on it I can see in Chrome that it dynamically changes from:
ourdomain.com/wp-content/themes/mm/pdf/Terms.pdf
to:
www.ourdomain.com/wp-content/themes/mm/pdf/Terms.pdf
When I view the source of the page the anchor tag is like this:
Terms
This works fine and renders the necessary pdf file. We have another url (http://www.ourdomain.com/contact) that also has a terms and conditions link that changes from:
ourdomain.com/contact/wp-content/themes/mm/pdf/Terms.pdf
to:
www.ourdomain.com/contact/wp-content/themes/mm/pdf/Terms.pdf
When I view the source of the page the anchor tag is as follows:
Terms
I have access to the entire site and the MySQL database for the site. What can I change to prevent the link from adding content to the URL? Are such links usually stored in the database?
Its because you didn't enter full path so the address is countine from your page address. as you see happend with the contact
/ will point to domain base
./ or empty will point to current url
Examples example.com/contact
<a href="/somefolder/file.pdf">
will point to http://example.com/somefolder/file.pdf
<a href="./somefolder/file.pdf">
will point to http://example.com/contact/somefolder/file.pdf
To avoid this you can enter the full url.
In case of wordpress if the link is in your theme you can use the function get_stylesheet_directory_uri() to get the full url for your theme.
Terms

Wordpress Permalink Issue Remove Auto Added Code

I have a Website of Wordpress In My Website When I click on any URL After complete Loading Content Page Some Codes Automatically Add on Permalink
For Example
http://example.com/wow-amazing/ Correct Link This Link Is Fine
After Complete Page Loading
http://example.com/wow-amazing/#.WfB5iI-Czcs
How Can I Remove #.WfB5iI-Czcs this Automatically Added Code From Permalink
Thanks In Advance
That part with # is generated at front side, so it actually doesn't affect you permalink. So there is no problem if you see that.
And actually it doesn't come from permalink, it is generated by some plugin's javascript. (for example AddThis generates such anchors, if you use AddThis, then the reason is AddThis)

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.

How do I make the Preview link on a WordPress post or page point somewhere else?

I've done a lot of small projects on the side lately where the client wants a good chunk of the website to stay the same, they just want to be able to edit particular "areas" of the site. Namely, some text in some box somewhere.
I've found that WordPress works for this pretty well. The interface is nice and friendly to use, and it's got lots of work behind it so I don't have to reinvent wheels, fix bugs, etc.
So what I wind up doing is making PHP pages which look like what the client wants, then making the content editable areas contain a particular post or page, which is what is editable in WordPress.
I'm having the particular page's contents displayed on the page using code like this
<?php echo apply_filters('the_content', $page_Contact->post_content); ?>
where $page_Contact is a variable defined elsewhere.
However, the "Preview" or "View Page"/"View Post" function on each page/post goes to the logical WordPress location and this is not the effect I want in this case.
So for example I have pages like this
www.site.com/index.php
www.site.com/about.php
www.site.com/contact.php
And so forth.
WordPress wants to have the "View Page" and "Preview Changes" links go places like
www.site.com/?page_id=2
www.site.com/?page_id=8&preview=true&preview_id=8&preview_nonce=45522671f5
Which is a problem because, in the permalink structure above, both of those go to the index.php page which, except for the page I'm using to structure index.php, it's not where I want the user/editor to go. And none of the cases above allow any sort of preview (which is a concession I'm willing to make given how I'm doing this)
Is there a way, preferably using a plugin to rig WordPress such that the preview for a page in the dashboard goes to a preview of the non-index.php page where the content will be housed? So for example the link for the "Contact" page in WordPress's dashboard goes to contact.php instead of ?page_id=2? This is a deal where I'm trying to get this done in the editing interface and I'm not concerned about the links in the site itself.
Obviously I would need to maintain this per-page and this would be a situation where new pages don't go up unless I put them there.
Take a look at the preview_post_link hook in WordPress -- it should be called when generating that link, and you can use that in conjunction with a custom field (or some logic) in order to construct whatever preview link you like.
Have you considered page links to plugin for wordpress? Also changing permalinks?

Categories