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.
Related
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 am developing my own theme for private use and i have a lot of scripts working with urls with extra parameters.
I'm finding my self adding a lot of empty pages to my site just to be able to connect some php file to specific url so i will be able to send parameters to that specific address.
Is there a better way i can have urls that can be associated with specific php files without adding pages or other things to the site. just like work out of the box when installing my theme?
You should try like this code:
//page url: http://www.example/myurl
add_action( 'init', 'my_rewrite' );
function my_rewrite() {
global $wp_rewrite;
//wp-content/plugins/myplugin/myurl.php
add_rewrite_rule('myurl/$', WP_PLUGIN_URL . '/myplugin/myurl.php', 'top');
$wp_rewrite->flush_rules(true);
}
More info:
https://developer.wordpress.org/reference/classes/wp_rewrite/
https://codex.wordpress.org/Class_Reference/WP_Rewrite
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 want to write a plugin to return HTTP 404 when user request license.txt, what is the correct action to hook (both efficient and effective way to block)?
Update:
Because I don't have control to the web server, I must do this as a plugin
Solution is actually pretty straightforward.
You need to create plugin which writes to .htaccess.
In the /wp-content/plugins create licence_redirect folder.
In that folder create licence_redirect.php file.
Paste code below to this licence_redirect.php php file:
<?php
/*
Plugin Name: Licence redirect
Description: Redirects license.txt. to 404
Author: J. Wrong
Version: 0.1
*/
?>
<?php
function lr_flush_rewrites() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function lr_add_rewrites() {
global $wp_rewrite;
$lr_wp_rules = array(
'license\.txt$' => '[R=404,L]',
);
$wp_rewrite->non_wp_rules = $lr_wp_rules + $wp_rewrite->non_wp_rules;
}
register_activation_hook( __FILE__, 'lr_flush_rewrites' );
add_action('generate_rewrite_rules', 'lr_add_rewrites');
Install and activate plugin.
In admin panel go to Setting -> Permalinks
Press save changes
From now on your license.txt requests will be redirected to 404.
If you can't create folders on server then you'll need to zip the plugin's folder and upload it using WP admin.
Cheers... counting bounty now :P
You can't. With a standard WordPress .htaccess, requests to static files are not passed to PHP at all, so there is no way to hook them.
There is a plug-in that will do this for you. Just set the file you want to redirect and its target. It even keeps a log
http://wordpress.org/extend/plugins/redirection/
I'm hosting an event and using WordPress to manage the site. We have a separate table (not WP user table) of attendees and I'd like for each of them to have a page on our site:
example.com/user1, example.com/user2, etc.
I've been reading up about WP_Rewrite but am a bit confused about how to conditionally redirect if the user exists, otherwise go about the normal flow.
Any help in achieving this would be much appreciated, thank you!
Edit:
There is a single one-off page that I'm trying to forward to. Right now, it's accessed with:
example.com/user?username=user1
If i understood your problem, you're going to create a page for each user, so in the permalink structure add tag %postname%, and maybe install no-category base plugin.
Check if the .htaccess it's created with the configuration that wp write for that structure, if it's not you can just copy and paste.
I ended up doing this by prepending a user's url with a tilde (~). The rewrite code is as follows:
// Uncomment only when new rewrites have been made
//add_filter('init', 'wds_flush');
// Leave uncommented
add_filter('rewrite_rules_array', 'wds_user_rewrite');
add_filter('query_vars','wds_query_vars');
function wds_flush()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function wds_user_rewrite($rules)
{
global $wp_rewrite;
$newRule = array(
'^~(.+)$' => 'index.php?pagename=user&uid=$matches[1]&act=show',
);
$newRules = $newRule + $rules;
return $newRules;
}
function wds_query_vars($vars)
{
array_push($vars, 'uid');
array_push($vars, 'act');
return $vars;
}