I am trying to make a news section for my homepage "index.php". So I use ajax to load "pages/news.php"'s content into a news div in index.php. This pages/news.php is supposed to pull out latest posts and format them nicely. The blog is in localhost:81/blog/
When I view localhost:81/pages/news.php, it can display the posts, no errors. But when I run localhost:81/index.php, it said
jquery-2.1.4.min.js:4 GET http://localhost:81/pages/news.php 404 (Not
Found)
If I remove this line in pages/news.php:
include(dirname(__DIR__) . '/blog/wp-blog-header.php')
index.php won't have error, but of course cannot view the posts.
If I copy the code in pages/news.php to index.php, it works (of course with appropriate including path). I am very confused here, how come using the same code but retrieving news.php content from ajax does not work but news.php itself works?
Could someone please advise me what has gone wrong?
Thank you for your time reading.
It turned out that this is a common issue: wordpress forces user to use ajax in a specific way. The instruction is here: http://rocketpopmedia.com/discover/2013/06/21/wordpress-ajax-and-the-404-error-youre-receiving/
I have created custom page in wordpress and in the page I have a form where users can upload a banner, and input some text.
The code is working perfectly on a local server. The problem is when I transfer to the live page, it is displayed as text instead of working normally.
I have tried to search for the page in cPanel but have later realized the pages are not stored as normal pages but stored in a database.
Please help me how to make my form work.
You can't put a php code (this is a code not a content) as a post content from the admin panel. A php file must be created (or using the functions.php) to serve that page and put the code there.
Find and open the functions.php file. It is located under the wp-content/themes/yourthemename/ folder.
At the end of the file (but before the ?> symbols if exist) add this code
function custom_function_for_your_page() {
// put your php code here
}
add_shortcode( 'custom_functionality', 'custom_function_for_your_page' );
Now as you guess, you can put the php code right below this line // put your php code here.
This is a shortcode. Now you can put that shortcode into the page content.
Open that page from the admin panel, where you've tried to put the php code. Instead of that code put this [custom_functionality].
Good luck :) Tell me if it doesn't work, we'll try more.
Looks like you may need a plugin, here is one such plugin I know about:
https://wordpress.org/plugins/insert-php/
I'm a beginner in PHP but an expert in programming in general, so I program C and C++ usually, but I have no experience with posting and getting with PHP, so I don't really understand the exact mechanism how this works.
Yesterday I worked for like two hours on my webserver and set a page on my Wordpress to make visitors upload a file. The page submits a form to a custom PHP page, but that page is empty and dull, and I would like to make the confirmation page in Wordpress itself. What I tried to do for that is that I created a new page, and used the plugin "insert PHP", and simply pasted the code from the page, to which the form submits, and made my upload form post to that Wordpress page. This doesn't seem to work.
How can I get this to work?
Simplifying the question: How can I make my form in wordpress post to another wordpress page and give the response in a wordpress page rather than a PHP page created from scratch?
If my question is not clear or requires any additional information to be answered, please let me know.
Thank you.
You're going to want to get to know specialized page templates in WordPress, and the page templating system in general. Ideally, you'd create a page in wp-admin and create a specialized page template for it.
If you're doing form processing, you can do that in the new template...but may be better off moving that logic to the functions.php file. Use action hooks to ensure that your processing functions are run on submit.
I'm trying to display a wordpress' plugin output with shortcode through do_shortcode().
It's gonna be displayed inside tooltip and the only way to call it, is by requesting a php file through ajax.
So I have a blank page called weather.php with the shortcode inside. Ofcourse it won't work like this since it's a non-wordpress file.
My question is.. what I have to insert in that file to make do_shortcode() work?
In other words.. wordpressize this page!
Thanks in advance!
Place this code at the top of the weather.php file
include_once("ADJUST/PATH/wp-load.php");
The wp-load.php file is found in the main directory of your WordPress installation.
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.