Wordpress loads wrong template with URL parameters - php

I have a strange problem with my site. It is a wordpress site working for a while now, and our company started some Adwords campaigns to boost views. Google Adwords adds a gclid parameter to the URL when clicked, and the site is loading another template (the default post template) instead of the template for the homepage.
I'd like to know, where to start searching for the faulthy parts? Any ideas?
Thanks!
PS: Not just the gclid parameter does this, but all other fake parameters too. Strange thing is that only the homepage does this, the other pages with structured URL-s are not doing so. The site uses Visual Composer plugin, if it is important. We tried to solve the problem by .htaccess file, but the redirect causes an error in Analytics tracking of Adwords visitors, so it is not a solution.

Finally, we found a solution!
The theme lacked the front-page.php template for the homepage (read more about wordpress template structure!), so I created it with the correct template HTML code according to the correct layout variable values, and it loads just fine. The URL parameters are not affecting the permalink structure anymore, because now we have a template for the homepage too.
This is better than doing hours of research for redirect- or permalink method workarounds...

if everything fails please use template_redirect hook to load any template you want... template_redirect hook
Also you have to send a 200 OK header there if you simply load file and exit.

Related

Best way to share content between a Wordpress site and a Drupal site

I will try to be clear but if you need more information please do not hesitate.
A site approached me to make a partnership together. They want to create a new section on their site, say a "blog" section. The goal is to post my blog post on their site, in this new section, without having access to their admin.
So I need to set up something to be able to automatically share the content I post on my site with their site. My site is on Wordpress and their site is on Drupal.
For now I see 2 possibilities, but tell me what you think and if there is better to do.
First possibility: Create the page /blog and all the articles (ex: /blog/article-title) on my site, then set up a reverse proxy to redirect their request to my server. I have no experience with network setup so maybe I am completely wrong. But even if it works, I don't know how to fetch only post content (not header, footer) and change all urls.
Second possibility: Create a Drupal module, which they will have to install on their site, which creates the page /blog and the custom content type named "blog" when it is activated. And then use the feeds module to get the content from my wordpress feed and create a blog post for each post in the feed. With this solution, I won't have any header or footer problem, and the urls to the articles will be generated dynamically.
Every advice will be usefull. Thank you.
IMO, the easiest way to do that is to use the Wordpress REST API.
You could get all posts with this https://yoursite.com/wp-json/wp/v2/posts and get a specific post with https://yoursite.com/wp-json/wp/v2/posts/:id
full doc here : https://developer.wordpress.org/rest-api/reference/
I would advise you to use the second possibility, cause there are already several drupal moduls in the wild the read and import feeds from other sites.
Even more, WordPress will provide your posts also in oembed-format. With this you can use this Drupal Modul: https://www.drupal.org/project/soembed
You vcan adjust this modul to allow content from your website and display it on the Drupal installation.

Empty links in Wordpress

I got my own created template in Wordpress. I got two pages : index.php and ab.php. My question is what should be a proper link code that links these two pages in menu? Because I always get 404 page or index.php page or blank. I try'ed everything including saving permalinks.
Here are a few of my examples:
http://example.com/?p=ab
http://example.com/?p=ab.php
<a href ="ab.php">
etc.
My guess is that this is your first WP theme.
If that is the case I should tell that the hierarchy from pure php websites won't work. Check this https://developer.wordpress.org/themes/basics/template-hierarchy/ WP uses the wp-admin part for creating new pages.
If you are switching from php to WP, I think I can help a bit by giving few pointers.
If there is an issue with your project I can't quite get what is happening but if you call http://example.com/ab.php the php inside will run however it's not cool to reference pages that way in WP.

Wordpress - creating custom pages with php code

I need to add few additional pages to my wordpress site.
These pages should not be "part of the site", ie they should not be linked somewhere from the posts and so on.
However, they should have the same header/footer as the rest of the site ( I am using custom theme ). And they should be accessible via url.
The final requirement is, I should be able to code in php.
At the moment, I tried to create a new "Page" in my admin console. And then write some php-code inside. However, all my php code gets commented and since not executed.
I don't think that installing plugins such as Exec-PHP is a good idea, so I am trying to find other solutions.
Any comments/advice/suggestions how to make it?
I would be grateful if you give me some how-to link.
Thank you in advance.
You can use a custom page template in your theme for this and just keep the site empty in the admin panel:
http://codex.wordpress.org/Theme_Development#Custom_Page_Templates
By using Template tag's you can create custom page.
write this code on the top of your php file....
/*
Template Name: Your Template Name
*/

Permalink redirect issue in WordPress

My WP website is currently set to use the following permalink structure:
http://websitename.com/%postname%/
If I change that it to:
http://websitename.com/%year%/%monthnum%/%day%/%postname%/
everything works correctly. Specifically, older blog posts posted elsewhere (e.g. my FB page) redirect to the new URL automatically.
However, if I use a custom permalink structure and set it to the following:
http://websitename.com/blog/%postname%/
the redirect does not work on older posts. That is, I can still access all of the posts from the site, but if I linked to one of my blog posts on my Facebook page, for example, the old URL produces a 404 and does not redirect. I am thinking it has something to do with "blog" not being one of the variables that WordPress recognizes.
Any thoughts on why this is happening, and how to fix it?
Thanks in advance!
You can use wordpress quick redirect plugin for redirection
http://wordpress.org/plugins/quick-pagepost-redirect-plugin/
It is simple to use

Make special pages from WordPress theme?

I'm trying to figure out how a certain WordPress sets things up. I'd like to have a special page where I could make WP calls and interact with the theme, without affecting anything else.
I just making test.php and putting it into my theme's folder, but that doesn't work.
#Eliran provides one possible option, but you could also add a page in the back-end of WP, just make sure it has the slug 'test', and change your 'test.php' filename to 'page-test.php'. If you're worried about the public seeing this, set the page visibility in the admin to 'private'.
Edit:
to move your understanding along a little further also, you should review the way that WordPress determines what file to grab to render a particular URL. This can be pretty confusing to start with, so be patient if you're not familiar with it, but it's at the heart of designing WP themes. I'll link to the examples, and if you scroll down a little there's a diagram that, along with the text, will help you see how WP is 'thinking'.
http://codex.wordpress.org/Template_Hierarchy#Examples
You can see here: Page Templates
all you need to do is create a page named page-{custom-name}.php and add it to the theme folder.
and inside this php file add:
/*
Template Name: My Custom Page
*/
and than to use this page you need to go to the wp-admin, add/edit a page and chose it:
inside the php file everything you do is classic wordpress.
all this is giving you is a custom page tamplate.
Put it in your root folder. When you go to look at it, you'd look at www.mywebsite.com/test.php
It may be other ways to do this, but I rather use the rewrite API and custom query vars, to create custom routes.
A previous answer on the subject can be found here
The basic idea is to add a new url rule, catch the query var with the parse_request filter and maybe do a die or redirect to prevent the default wordpress template from loading.
I prefer this over theme templates, because with templates you need to create a page for each new url, and if that page gets acidentally deleted, that functionality would stop working.
What Pages are Not:
Pages are not Posts, nor are they excerpted from larger works of fiction. They do not cycle through your blog's main page. WordPress Plugins are available to change the defaults if necessary.
Pages cannot be associated with Categories and cannot be assigned Tags. The organizational structure for Pages comes only from their hierarchical interrelationships, and not from Tags or Categories.
Pages are not files. They are stored in your database just like Posts are.
Although you can put Template Tags and PHP code into a Page Template file, you cannot put these into the Page or Post content without a WordPress Plugin like Exec-PHP which Read overwrites the code filtering process.
Pages are not included in your site's feed.
Pages and Posts may attract attention in different ways from humans or search engines.
Pages (or a specific post) can be set as a static front page if desired with a separate Page set for the latest blog posts, typically named "blog."
More About Pages.
In WordPress to add a new page you have to log in to the admin/backend and from the pages menu you can add a new page. In this case, you can select templaes for your page and also you can create a custom page template for that page.
You may read Createing a new page in WordPress. and custom Page template in WordPress.

Categories