I am working on a cardealer website but there is a problem with the URL/permalink. When you want to share the page it doesn't use the actual URL but the permalink set on the page. I know this sounds logical but the URL is created in a external PHP file. This PHP file get's the information of the car out of the database and shows it on the page. The actual URL also get's build in this PHP file. So the permalink set in WordPress is a default and the actual URL is what I would like to share.
Permalink set in WordPress:
Actual URL on the page:
So if someone shares this page I want to send the actual URL (https://www.prinsauto.nl/vakgarage-aanbod-details/audi-a6-avant...) but not the permalink. Again, the actual URL is build in a PHP file.
How can I do that?
WordPress is not very easy when it comes to custom URLs. Viewing the code of the template-loader.php file, it seams that you could use a template file to run your page functionality.
As can seen in the code (line 77) you can use the template_include filter in your plugin (or template's functions.php file) to check for your specific "route" and return the template file to load.
Example:
add_filter('template_include', 'your_function_name', 0, 1);
function your_function_name($template) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, 'vakgarage-aanbod-details') !== false) {
return 'template-file-name.php';
}
else {
return false;
}
}
All the PHP logic you described in your question should go in included template file.
Related
I need to add some of my own php files to an existing Wordpress site (from Bitnami) so it can be accessed from a 3rd-party service. I don't need to access anything from Wordpress in my code, but I need my code to be accessible from a url for 3rd-party services.
Like this:
https://myWordPressSite.com. <- normal WP site
https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- my code
How can I add my directory of code to the Wordpress file structure, and how to access it from a URL?
You can add a $_GET OR $_POST to wp in the plug side
if you want I would add a code to help with not getting hack
Your url https://myWordPressSite.com?name=yes
function getdata(){
if(($_GET['name'] == "yes") || ($_POST['name'] == "Run")){
}
}
This is one way
Good Luck
Karl K
Create a folder in your theme's root folder called myCustomDirectory and place your php file inside that.
then add to your functions.php the following:
This will handle the Rewrite Rules:
add_action('init', 'custom_add_rewrite_rule');
function custom_add_rewrite_rule() {
add_rewrite_rule('^myCustomDirectory/generateSerial.php', 'index.php?generateSerial=true', 'top');
}
This will handle the Wordpress Query Vars:
function add_query_vars_filter( $vars ){
$vars[] = 'generateSerial';
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
This will tell wordpress to pull the correct file:
function load_custom_template($template){
if(get_query_var('generateSerial') ){
$template = get_template_directory() ."/myCustomDirectory/generateSerial.php";
}
return $template;
}
add_filter('template_include', 'load_custom_template');
make sure to flush your rewrite rules when you are done by going to: Settings > Permalinks and clicking 'save changes'
It turns out it was much simpler than I thought, or any of the other answers presented here.
The answer is to just add a directory of my own code inside the Wordpress home folder.
In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:
/opt/bitnami/wordpress
So I created a directory and put my code in it..
/opt/bitnami/wordpress/myDirectory
/opt/bitnami/wordpress/myDirectory/generateSerial.php
/opt/bitnami/wordpress/myDirectory/doSomething.php
And it was then accessible from a url like:
https://myWordPressSite
https://myWordPressSite/generateSerial.php //etc
One small catch is that some Wordpress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation.
Hello Everyone and thank you for your help!
I have an external (outside wordpress in root directory) php file that I would like to use the custom_rewrite_rule function to make the url pretty as well as pass on information for variables that will look up an API record from that URL. You might ask why I haven't done it the regular way and made it a page template inside wordpress, and that is because I am using a custom JQuery slideshow that seems to have trouble running inside a wordpress environment.
So what I have in my child themes functions.php is:
function custom_rewrite_rule() {
add_rewrite_rule('^yacht-details/([^/]*)/([^/]*)/?','yacht-details.php?info=$matches[1]&boatid=$matches[2]','top');}
add_action('init', 'custom_rewrite_rule', 10, 0);
and in the php file am using the standard $_GET to acquire the information.
$info = $_GET["info"];
$boatid = $_GET["boatid"];
My actual problem is what I am actually getting is literally $matches[1] and $matches[2] instead of the variables.
What am I doing wrong? Does this need to be done with .htaccess instead of functions.php? The problem I run in to going that route is wordpress does not seem to like more than one subfolder.
Thank you again.
I've been looking for hours trying to find a straightforward answer to this but I just can't seem to find anything out there which tackles this issue. There are numerous examples for rewriting standard URLs, but nothing in-terms of rewriting pages from within a custom plugin.
I've created a custom plugin with a special directory in it called test. The test directory has several pages which are blah.php and shmeh.php. Is it possible to create rewrite rules to these pages i.e. http://example.com/blah, http://example.com/shmeh
Thank you very much for all the help.
So essentially what you are looking for, is to have Wordpress load a specific page template (blah.php) when the user visits http://example.com/blah/. Also, you need it to to be completely generated in your plug-in without actually creating any pages or posts. You are on the right track. What you want to do here is create a rewrite rule that utilizes a specific query var to load the page. Here it is step by step:
Step 1: Rewrite rules
http://codex.wordpress.org/Function_Reference/add_action
http://codex.wordpress.org/Plugin_API/Action_Reference/init
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
/* Rewrite Rules */
add_action('init', 'yourpluginname_rewrite_rules');
function yourpluginname_rewrite_rules() {
add_rewrite_rule( 'blah/?$', 'index.php?blah=true', 'top' );
}
So in the above code, your just telling Wordpress that example.com/blah/ should be treated as if the user were visiting example.com/index.php?blah=true. Now "blah" is a query var that you will set up in the next function below. Query vars are just what Wordpress calls $_GET variables. However they must be registered with Wordpress so it can properly store the value (which is "true" in this case). We will do that below in the next function. It's also worth noting the third setting in the function "top", which gives this rewrite rule priority over other rewrite rules. Alternatively, setting it to "bottom" would check that no other rewrite rules match first before using this one.
Step 2: Query Vars
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
/* Query Vars */
add_filter( 'query_vars', 'yourpluginname_register_query_var' );
function yourpluginname_register_query_var( $vars ) {
$vars[] = 'blah';
return $vars;
}
In the code above, your just registering the query var "blah" with Wordpress. That way, when someone visits example.com/blah/, which your rewrite rule tells Wordpress to treat it like they are visiting example.com/index.php?blah=true, Wordpress actually bothers to save it's value (which is set to true). This is so you can use it in the next function, which loads the template you want when users visit your url.
Step 3: Template Include
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
http://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query
http://codex.wordpress.org/Function_Reference/plugin_dir_path
/* Template Include */
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1);
function yourpluginname_blah_template_include($template)
{
global $wp_query; //Load $wp_query object
$page_value = $wp_query->query_vars['blah']; //Check for query var "blah"
if ($page_value && $page_value == "true") { //Verify "blah" exists and value is "true".
return plugin_dir_path(__FILE__).'test/blah.php'; //Load your template or file
}
return $template; //Load normal template when $page_value != "true" as a fallback
}
In the code above, you are simply telling Wordpress to use your blah.php template any time the query var "blah" is present and set to true. In this case it is present because a user is visiting a url with a rewrite rule that utilizes the "blah" query var. The function checks if the "blah" query var is present, then checks if "blah" is set to true, and if so loads the template. Also note that the template path is test/blah.php. You are using plugin_dir_path(__FILE__) to relatively include the template based on the location of your plugin. That way it's relative as a plug-in should be.
Step 4: The Actual Template (blah.php)
http://codex.wordpress.org/Function_Reference/get_header
http://codex.wordpress.org/Function_Reference/get_footer
http://codex.wordpress.org/Function_Reference/get_sidebar
Your template file will need to load the header, footer, and sidebar itself. When using template_include, Wordpress will not automatically load the header and footer as if you were using a page template. Be sure your template file calls get_header(), get_footer(), and get_sidebar() so that you get your complete layout.
Notes
Hope this helps, I was searching for something similar the other day and had to figure it out the hard way. It's also worth noting that I have prefixed all of the functions as "yourpluginname_". You can change this to whatever you want your prefix to be, but be sure to update the function names to match as well.
I am currently working in a application like wordpress. i need a php class to handle the url request for eg : if u load the below url in browser
http://somename.com/myaboutpage/
the handler should load the file about-us.php from my theme folder
and if i use the below url
http://somename.com/action/login/
the handler should trigger the file login-action.php from my application core and return the values
how its possible..
i tried to study how elgg & wordpress handle the request. but i am unable to get the exact..
so please help me.
Note the pages i create will be dynamic so i need a page handler fully dynimic like word press permalink handler
Have you set a BASE_URL in an index.php file? if you have, you could check to see if that is set, if it isn't it means the user has somehow managed to navigate directly to your about-us.php file. So something like
if(!defined('BASE_URL'))
{
require('/*your index.php file*/');
header('Location: BASE_URL')
exit;
}
I now need to make a Kohana 3 site have a Wordpress blog.
I've seen Kerkness' Kohana For Wordpress, but it seems to be the opposite of what I want.
Here are the options I have thought of
Style a template to look exactly like the Kohana site (time consuming, non DRY and may not work)
Include the blog within an iframe (ugly as all hell)
cURL the Wordpress pages in. This of course means I will need to create layers between comment posting, etc, which sounds like too much work.
Is there any way I can include a Wordpress blog within an existing Kohana application? Do you have any suggestions?
I found this post detailing the Kohana for Wordpress plugin, but I am still confused as to how it works.
Does it mean from within Wordpress, I can call a Kohana controller? Is this useful to me in my situation?
Oh, I did this a long time ago (actually towards the end of last year).
Assumptions
You are using Wordpress permalinks with mod_rewrite or a similar option.
You don't have register_globals() turned on. Turn it off to ensure Wordpress's global variables don't get removed by Kohana.
Renaming
First, you need to rename the __() function in Kohana. Say, you rename it to __t(). You'd need to replace it everywhere it appears, which if you use an editor like Netbeans that can find usages of a function or method is pretty easy.
Hierarchy
The next decision you need to make is whether you want to load Wordpress inside Kohana or Kohana inside Wordpress. I prefer the latter, which I'm documenting below. I could document the latter if you'd prefer to go that route.
I put the kohana directory in my theme directory.
In your functions.php file of your theme, simply
include TEMPLATEPATH . '/kohana/index.php';
Kohana Configuration
Your Kohana's index.php file also needs some work. Remove the lines that look for install.php as they will load ABSPATH . WPINC . 'install.php' instead and display an error message in your wordpress admin. You also need to change the error_reporting as at the moment Wordpress fails E_STRICT.
You will very likely need to remove the last few lines of your bootstrap (in Kohana) that process the request, and change your init:
Kohana::init(array(
'base_url' => get_bloginfo('home') . '/',
'index_file' => '',
));
In either your Wordpress functions.php file or in your bootstrap, add these lines:
remove_filter('template_redirect', 'redirect_canonical');
add_filter('template_redirect', 'Application::redirect_canonical');
where Application is a class of your choosing.
My code for the Application class (without the class definition) is:
public static function redirect_canonical($requested_url=null, $do_redirect=true)
{
if (is_404() && self::test_url())
{
echo Request::instance()->execute()->send_headers()->response;
exit;
}
redirect_canonical($requested_url, $do_redirect);
}
public static function test_url($url = NULL)
{
if ($url === NULL)
{
$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
$url = trim($url, '/');
}
foreach (Route::all() as $route)
{
/* #var $route Route */
if ($params = $route->matches($url))
{
$controller = 'controller_';
if (isset($params['directory']))
{
// Controllers are in a sub-directory
$controller .= strtolower(str_replace('/', '_', $params['directory'])).'_';
}
// Store the controller
$controller .= $params['controller'];
$action = Route::$default_action;
if (isset($params['action']))
{
$action = $params['action'];
}
if (!class_exists($controller))
return false;
if (!(method_exists($controller, 'action_' . $action) || method_exists($controller, '__call')))
return false;
return true;
}
}
return false;
}
which lets Wordpress do it's redirect for any page that may have moved e.g. /about/calendar to /calendar as long as you don't have an about controller and calendar action defined.
So there you have it. Any urls not defined within Wordpress will fall to your defined controller (or use your theme's 404 template).
Additional
This isn't required, but you could put your theme's header.php under your kohana views folder (application or in a module) and from any of your theme files
echo View::factory('header')
You could do the same thing with your footer (or any other files for that matter). In your header.php, you could also do this:
if (isset($title)) echo $title; else wp_title(YOUR_OPTIONS);
That way you could in your controller
echo View::factory('header')->set('title', 'YOUR_TITLE');
To keep urls consistent, you may have to take off the / from the end of Wordpress permalinks so /%year%/%monthnum%/%day%/%postname%/ becomes /%year%/%monthnum%/%day%/%postname%, etc
Please let me know if you need any more help integrating Wordpress and Kohana.
I've actually used wordpress for the CMS of a code igniter site. This is the method i used to pull page content, not blog content, but maybe you can change it up a little to fit your needs.
In my front controller I added the wordpress header file
require('/path/to/wp-blog-header.php');
This gives you access to the 2 functions you'll need
get_page() – Get the page data from the database
wpautop() – Automatically add paragraph tags to page content
To get page data
$page_data = get_page( 4 ); // Where 4 is the page ID in wordpress
If you get this error:
Fatal error: Only variables can be
passed by reference…
You have to do it like this
$page_id = 4;
$page_data = get_page( $page_id );
because of a bug in certain versions of php
Then in the view
<?= wpautop($page_data->post_content) ?>
Hope this helps
EDIT
I installed wordpress at /blog in the filesystem. So wordpress actually runs as a blog normally. I just use this method to grab the pages
This is going to be extremely difficult, because of the way WordPress works. Specifically, it uses global variables all over the place, and because Kohana is scoped, you will not be able to access those variables.
Long story short: what you want is nearly impossible. However, if you get it working (without hacking WP), I would be really interested to see how you did it.
See here: http://www.intuitivity.org/archives/8
I figured it out yesterday :)
Another solution is to keep both Wordpress and Kohana installations completely separate. Then you create a custom Wordpress theme that will pull the header and footer from Kohana (you can create a Kohana controller for that).
Once you have the header and footer in, the blog looks integrated to your website even though it's still a completely separate installation. The advantage is that there's nothing to hack to either Wordpress or Kohana to get it working.
There's some more details about this method in this blog post: Integrating Wordpress into a Kohana application
I always thought this would be relatively easy. That is, to use WordPress as your site's back-end (for the blog part, at least) and use Kohana for serving up posts and pages. If I'm not mistaking, all you would need to do is set up your models (post, comment, page) to gather their data from the WordPress database (with or without ORM) instead of a new one.