I commonly run into a scenario where "the powers that be" want an exact copy of a page in multiple places on a website. Rather than actually duplicate the content, all I do is override the section in the nav that is highlighted, and then include the page. The final page looks something like this:
<?php
$top_nav_item_id = 'teen';
include('../interests/teacher-resources.php');
?>
This typically works. I am trying to duplicate this for a blog category, done in wordpress. All I seem to get is a blank page, no matter what I do. I've tried all of the following lines:
<?php
include('../blog/index.php');
include('../blog/type/teen/index.php');
include('../blog/type/teen/');
include('../blog/');
?>
Does anyone have any ideas? Is this a URL rewriting thing? Do I have to include the template file for that particular category?
Any help is appreciated.
PHP include expects files, not URLs, so it doesn't have access to the URL namespace exposed by WordPress. Those files don't exist on-disk; mod_rewrite first turns the pretty URLs into an internal request to index.php, WordPress figures out what you really wanted based on the original URL, fetches a bunch of stuff from the database, then produces the page.
This is a pretty complicated topic, and one that isn't very apparent from the start. This page should help you get started. The key is to include the WordPress blog header - explained on the linked page. You'll probably also want to check out the WordPress Codex for resources on using the WordPress engine's API.
ini_set('display_errors', true);
error_reporting(E_ALL);
No idea what's going wrong, but it does. Maybe Wordpress can't find it's environment, maybe some variables are being overrided... Actually it's a bad idea to include solutions like wordpress, because you never know, what global variables, functions, classes will intersect.
PS: And, by the way, include uses file system paths but not URLs.
For similar issues I use iframes to include the copy of the content. You can write the original page to look for an "?embed=1" flag in the url, and only include the embeddable content in the main page when the embed flag is present (so you can leave out toolbars and frames that would be redundant.) So the iframe src url would use the ?embed=1 tag to embed the content.
This solution is a bit of a hack, but then, the problem is a bit of a hack to begin with.
I received a good explanation of why I couldn't include the blog page, but not any alternatives that would work for me.
My final solution was to modify the category template for that page directly. As stated originally, I use $top_nav_item_id to control which menu item is highlighted in the nav, to give the appearance of the page belonging to that section. Rather than override this, I simply made it conditional on a query string. As long as the user is following legit links on my site, they will get the correct query string and have no problems.
$_POST is disabled in Wordpress. $query_string (built into WP) uses some sort of caching, and would always display as it was first loaded.
Final solution:
if(strtolower($_SERVER['QUERY_STRING'])=='display=teen') {
$top_nav_item_id = 'teen';
} else {
$top_nav_item_id = 'programs';
}
Thanks to all who tried to help.
Related
First of all, I want to make it clear that I know what best practices are when it comes to handling 404 errors. However, I have this specific case where I may need a tailored approach.
I'm handling a newspaper site that has more than 10 years' worth of archives, with 150k+ hard-worked pieces of content and loads of links that still get clicked through. It also went through a lot of trouble: 3 different CMS's before WP, each with it's own link structure and unproper redirecting upon every change. So now the archives are all but "lost" from a SEO point of view.
With more than 90% content misplaced, showing classical 404's is not really an option. The emergency exit was to redirect the words in the URL to a search query (after filtering out the constants) and hope for the best. In most cases, the relevant result shows up towards the top, but not always. For this reason, I suppose it's wrong to pretend that the 404 is simply not there.
The other approach I thought of was this: keep the URL verbatim, send the 404 status, but use the 404 template to show a search query (WP_query with 's' parameter) on the relevant words.
This has the advantage that, on strong matches (those that are all but certain to be the "I'm feeling lucky"), I can decide to force an actual 301 redirect.
That's not always the case, though: sometimes the actual wanted article is very far down the list. Still, it would work almost fine, except that for some reason pagination is not working on 404s. So now I think one of two things need to be done:
The simple solution, if only possible: somehow make pagination work on the 404 template - since I have no idea why it doesn't already, I don't know if it can be done or how. (Update: most likely it is because the pagination query var/slug is treated as part of the search)
The complicated solution, if only feasible: Use the search template itself. The 'search' slug can be removed completely by hooking into rewrite rules with a $wp_rewrite->search_base = ''; This theoretically turns almost any url thrown at it into a search. The huge problem is that it also does it for postnames and everything else except categories and tags.
So what I get from this is the following: Whenever there's a URL request, Wordpress will look if there is a category matching, then a tag, then it will do a search. Only after that will it look for matching authors, archives, posts etc. If only I could somehow hook into wordpress's internal rules regarding url parsing priority and move the search thing to the end of the list, the problem would be solved.
I'll have to admit that I didn't try any actual code. I don't know where to start from, I don't know exactly what to search for and there also seems to be little documentation for what I want. All I was able to do so far was blind test, as described above.
So the question is if there's any way to do either of the above and how.
The simple solution, if only possible: somehow make pagination work on the 404 template - since I have no idea why it doesn't already, I don't know if it can be done or how.
It's hard to say why pagination isn't working without seeing the code for your 404 template.
The complicated solution, if only feasible: Use the search template itself.
You can use the template_include filter to change the template. You'll also have to manually change the main query to a search query:
add_filter('template_include', function($template) {
if(!is_404()) {
return $template;
}
$search_query = new WP_Query(array('s' => get_query_var('name')));
if($search_query->have_posts()) {
// Replace the main query with the search query
global $wp_query;
$wp_query = $search_query;
// Change the response code
status_header(200);
// Use the search template
return get_search_template();
}
return $template;
});
Note that under normal circumstances, the best practice for modifying the main query is to use the pre_get_posts filter. In this case however, we don't know whether or not this is a 404 until after the query is executed.
Also, I'm using status_header to change the response code from 404 to 200 if the search returns results. If all you are trying to do is serve the right content to users then the response code probably doesn't matter.
In most cases, the relevant result shows up towards the top, but not always
If you decide that you do want to just serve the first result of the search, you can update the above code to redirect:
if($search_query->have_posts()) {
$url = get_permalink( $search_query->posts[0]->ID );
wp_redirect($url);
exit;
}
Update: Also, you could just redirect the request to a search without having to worry about modifying the 404 template or loading a different template:
if($search_query->have_posts()) {
$url = get_search_link( get_query_var('name') );
wp_redirect($url);
exit;
}
You could tweak your original idea:
In most cases, the relevant result shows up towards the top, but not always. For this reason, I suppose it's wrong to pretend that the 404 is simply not there.
You can redirect the user to a copy of your search page with some added messaging along the lines of "This page has moved, is it one of these?" (Or, even better, dynamically add that messaging to your standard search page if the user was redirected).
Depending on how your search is set up, you can send over the original URL as a php POST variable to run the search, or parse it on the 404 page and send it as a series of GET variables.
Or am I misunderstanding some limitation in parsing your URL and submitting it in the wordpress search?
So, I've read The Loop and I got the hang of it. (I created a page that lists the 3 most recent posts, a page that shows a page (as opposed to a post), and so on.)
However, it seems silly that, in order to integrate a WordPress blog into an existing site, I'd have to replicate all the different kinds of pages (lists of posts, the posts themselves, pages, etc.)
Is there a generic way of doing this?
The full idea is something like this.
The blog itself (with its ugly default template) is installed at http://blog.example.com.
I want to integrate the blog into an existing site, at the URL http://example.com/blog.
I'm doing a mod_rewrite that forwards the entire query string, e.g. http://blog.example.com/?p=7 gets rewritten to http://example.com/blog?p=7.
Now, in my PHP code at http://example.com/blog, I want to do something like this:
query_posts($_GET);
// display results
wp_reset_query();
That's where I'm stuck. Basically, I want to display exactly what's in the "content" area of a default WordPress template, on my site, according to whatever the query string dictates. Almost as if I were just using an iframe. I would rather not have to parse the query string to figure out whether I have to loop through this or that or turn off the $more global or not, etc.
What am I missing?
Instead, should I be installing the blog at http://example.com/blog and creating a template that mimics the website? This seemed complicated so I didn't go this route, but maybe someone should change my mind.
Appreciate any help, including advice for alternative designs.
Have you considered outputting the blog into a full rss/ outputting the blog as json and then pulling it in that way?
I'm starting with "PrestaShop" and I just can't figure out, how to put a link in template to custom page I created in CMS module... I thought, there might be some easy way, as there is in WordPress, like "get_permalink(ID)", but there's nothing like this and I can't find anything about this anywhere and it just drives me mad.
So, here's the deal, I've got a custom template, and there are some top links, like "About Us". I've created this page in CMS and it has ID "6".
How do I make this bloody "PrestaShop" to generate a link to this page in my template file?
About
I think you're looking for SMARTY template tags and custom variables defined for Prestashop specifically. The one the you probably need is {$base_dir} which will be translated to http://www.yoursite.com/ obviously with appropriate protocol (non-secure HTTP or secured HTTPS).
After that, you only need to include page URL, which you can get from Admin->Tools->CMS section.
If I find any specific tags that you can use to call the content, I will update my post here.
Your Text
the "WHAT goes here" is just the url you want your link drives the client to when clicking on that link.
You need to understand a little minimum of HTML for this I guess. Check html tag a meaning .
Prestashop has a fairly good and extensive documentation. Two and half seconds googling drives me here, just like answering your question it looks like.
I am beginning to develop a website as a personal project, to discover a little bit web technologies. My question is more about "how to make the things clean" than a technical question.
Here is the thing:
Let's say that an index.php page will include an other menu.php, which will permit to navigate the website.
My index.php look like this, basically:
include("header");
include("menu");
include("DEPENDING ON WHICH LINK AS BEEN CLICKED FROM THE MENU");
include("bottom");
To avoid the POST method, and have a lot of stuff on my URL, I would like to do it an other way, but I do not know what is the best one.
I thought about declaring a variable like $page, when a link is clicked, I can do something like "if $page == home", then I include the home.php page... etc...
I do not know if my question is clear... I know that it will appear as a very easy and beginner question but I don't even know where to look...
Do you know if I can find any "open source website" so I can study the code and see the best practices about it?
P.S.: Sorry for my english which is probably not perfect at all, I am working on it.
You can have a menu like
Home
About
Then on your PHP code
include $_GET["view"] . ".php";
Note that I am not validating, so any parameter passed on the url would be able to include any file.
The $_GET returns the values passed to the page through the URL.
The $_POST returns values posted.
The $_REQUEST returns both $_GET and $_POST values.
A good place to study many languages is W3Schools, you could check there sometime.
Make a page which will be common redirect page.
Every post will come to that page and based on the page parameter it will redirect.
So action of every page is same, but based on page paramter redirect to which ever page you want
You can switch case and
use header to redirect
i think you want to avoid GET method and avoid lot stuff in url
For learning
I thinks this is the simple website for learner.
http://www.w3schools.com/php/default.asp
http://www.plus2net.com/php_tutorial/site_map.php
http://www.tizag.com/phpT/
actually most of all these websites are same.
i hope you know the basic website PHP.net
Then,,.. no one is low level ..every low level will be in a top level one day.. just like you am also trying :)
Don't do what you are trying to do. The whole point of having pages is to handle things with different files. That is, you will have some commonality between files (handled by auto prepend and include path, potentially) such as your header and footer. Each file should include this on its own and print it out directly.
That is, you should not handle everything on one page and then conditionally include a file. Just send users to a different page.
Finally, I recommend not splitting up the header/footer files at all. Instead create a decorator that wraps the main content and displays it all at once. Something like:
$page = <<<HTML
<html><head><title></title></head>
<body>
<div id="top nav"></div>
{CONTENT}
</body>
</html>
HTML;
Then you go through and build your page content. Then you add it to CONTENT in the decorator and print it. PHPTAL is a great way to have this handled externally.
Hi you should please ask one question at a time:
I think this basic tutorial will give you a good idea on how and what to use the include(); func.
http://www.w3schools.com/php/php_includes.asp
I started with:
http://www.solitude.dk/filethingie/
Very simple .php file administrator.
You should definetly check out sourceforge, giant colletion of open source projects just filter by php (search for literally anything).
Just wanted to mention that you can download the full code of more complex pages (that are based on php) like
wordpress (blogging platform) - very easy to install and configure
identi.ca (twitter open source alternave)
You can now download reddit´s source code - quite easy to.
Maybe you wont be able to modify them immeditelly but theyll help you to get the picture
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.