How to add dynamicly linked and created pages in wordpress? - php

Since I had some comments on the formulation of my question, I decided to rewrite it completely. And also, I understand a little bit more about wordpress so it should make more sense now.
So, let's start with what I'm trying to accomplish by using a simple example:
Goal:
Make a search engine for some products and link to them using a database to generate the information.
Result:
http://my.site.url/wordpress/<plugin-name> <-- The page with the "search" and "search result
http://my.site.url/wordpress/<plugin-name>/products/<product_name> <-- When click on a search result
Part 1:
I want to make a plugin with the name "plugin-name" Right now, I put my code in
http://my.site.url/wordpress/wp-content/plugins/<plugin-name>
On the activate, I create some tables with some data. Since there is no page yet, I create 1 on the fly using the API call 'wp_insert_post'. I give the name and slug "myPlugin".
So, when I go on the main site, I can see my newly created page "myPlugin". But right now, there is no content attached to it. To do so, from what I understand, I need to create a page in my plug-in directory named "myPlugin.php".
http://my.site.url/wordpress/wp-content/plugins/<plugin-name>/myPlugin.php
So, how do I link the page I created manually and the source file in the plugin directory?
Part 2
When clicking on a link in the search result, I would like to have a page "product.php" and display its information. So, is it possible to create a "temporary url" for the result using sub pages (see result at the beginning)? And if possible, how can we achieve that?
Thanks in advance and I hope it's clearer.

u can find how to create plugin from
http://ditio.net/2007/08/09/how-to-create-wordpress-plugin-from-a-scratch/
in plugin write function for search(as per u r logic) fetch result
create a custom page refer video from www.wordpressmax.com/customize-wordpress/custom-page
in custom pages template in between get_header(); and get_footer(); call search function and display records.
Thats it.

thisMayhem is right. This sounds like a bunch of nonsense. But, if you're trying to make a WP page that executes some custom PHP, look up the WP template hierarchy.
What you can do is this:
create a WP "page" - call it "Search results."
create a template directory file called page-search-results.php (filename corresponds with slug of page from step 1)
put some generic header/footer stuff there (see other template files - most often it's just a question of putting in <?php get_header(); ?> and <?php get_footer(); ?>)
put PHP code in that thar page. in them hills.
if you use a caching plugin, may want to put an exemption for /search-results/
So, when someone pulls up http://your.site/search-results/ - your custom PHP code will execute. It's up to you to make that logic and communication happen - whether via $_GET or $_POST, etc.

I'm sorry you're experiencing some problems. Unfortunately as we say above because it's a 'one off' for a client project we can't support it - it works for them :) Feel free to rip it apart and retest and if you have any success let us know.

Related

How to "creating new page" works in PHP?

Page creating in PHP: I'm trying to learn how to make a cms but I couldn't understand something. For example in Wordpress when you click "create a new page" button what is happening at the backside? For example now I don't have any page.
I want to create these pages dynamically:
Home
About
Contact
After I want to add this pages in my menu. What is the best way to do that?
Questions:
Should I create a new php file to do this or just get data from my database which belongs to each page?
How I add these pages in my menu dynamically ?
What is htaccess? I couldn't understand that exactly it does relating the the processes of the files.
There are tutorials on internet but they always show how to get articles from database. When I have another item (for example a slider) I need a real page.
Can somebody suggest me a simple tutorial, or provide a clear explanation which explains the logic simply.
I have created something similar. This is how I did it (may not be the best way but it is certainly the easiest).
Create a template file and insert tags (!Title!, [[Title]], etc) where you want the new pages data to be inserted.
When creating a new page, read the contents of the template and replace the tags with the data submitted (I used titles, desc, h1, etc but this can also be achieved through a simple content function)
The user selects/creates the directory using mkdir()
All this is then stored in DB ready for the user to navigate to.
You can then use the DB entries to build a navigation menu

Wordpress: How to use get_page/is_page to display certain pages

Atm. i'm working on a Wordpress website that requires a bit of rethinking in terms of showing page content.
Atm im using
<?php if (is_front_page()) { ?>
show this content
<?php } elseif (is_page('contact')) { ?>
show this content
<?php } else { ?>
show this content
And thats working fine no problem at all. (and i want to keep this for the base structure of the site)
Basic info:
- The rule is in the header
- the rule also defines whether to show a slider or not in the top.
- Allot of content is dynamic in the header
The real problem shows later on when i want to code a bit smarter for not having 999999 lines of is_page for every case subpage there is, on the portfolio.
Right now i have to make a line of "is_page" every time i want to create a subpage* and thats very ineffective coding i'd say, allthough the design content requires it to change content depending on where u are on the website.
The reason why i need to create a new is_page for every subpage is due to the top changes color and different content shows based on the page ur in. the "else" state defines only if i create a new basic page not a case page.
What i want to achieve is if u land on the page "case1" or "case2" etc unlimited number i want to display specific content.
So
What im looking for is either a way to create a is_page rule to define if u are on case1-case9999999 show this div
Or
Using get_pages to create the rule so i keep using is_page for the base structure of the site and then add a extra rule that controls which content shows on case1-case99999999
If i somehow didn't explain this good enough fire away a comment and i'll do my best to enlight the issue.

Insert .php file in another .php file and only execute a function()

I know the title is messed up, but basically I have: news.php which is kind of a dashboard where I get all news from DB and I can perform actions on them (update, delete, insert new) and also I display them in a traditional way (tables). The problem is I want the title of the first news to be displayed on my homepage(footer), but when I include the news.php file withinclude(), the hole code it's included and on the home page I get all the things I have in news.php, all the news, the buttons (edit, add, delete) and everything else. Is there any way of including one .php file but not displaying it ? I want to create a function that only displays the title of the first news but I can't because the hole code is imported and displayed.
Thank you for your quick answers. Having only this small problem, I'll just create another file where I get the latest news without using frameworks
You need to create a function library so that you can call specific segments if you want.
But here's a quick shortcut. copy news.php into news2.php. go into news2.php delete all the parts you don't want; include news2.php instead
You could try making another single file called firstnews.php which would only retrieve that particular first news item and include that.
You should maybe read up on MVC frameworks, its a good way to seperate data retrieval, manipulation, and then actually showing the data. For example one php file would hold a function that retrieves all news item, and another function that only gets the latest news article. Then the actual file which shows the user data would simply choose which function it wants to use.
Its a lot to get into, but the codeigniter framework is a great way to get familiar with MVC frameworks and it will open a lot of doors for you.
CodeIgniter Homepage
Manual on how to setup and use CodeIgniter
For now though try just making a seperate php file that only retrieves the latest news item instead of all of them and include that.
Hope this helps!
-Gui

Prestashop - custom template link

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.

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