wordpress plugin access file directly in browser - wordpress

I am trying to access a plugin file directly in the browser, so i can run a cron.
When i go to the correct url, i am getting a page not found error.
Does wordpress by default prevent you from accessing this directly? Is it maybe something to do with the .htaccess, or should i be able to view this directly?
I am trying to access the file located like this (just an example):
http://mywebsite.com/wp-content/plugins/askimet/askimet.php
Any help would be greatly appreciated!

WordPress does not prevent you from accessing PHP files directly. However, the PHP files themselves usually do. This basically makes sure WordPress is loaded.
In your example, akismet.php has the following
// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
exit;
}
You probably do want WordPress to be loaded for your plugin, though.
Instead, you need to make your plugin know of a URL construct which you can detect and intercept. For example, say you visit the page example.org/?my-plugin-action. Your plugin should check for this on normal page requests (such as init or template_redirect) and if it is found, call your PHP script, then call exit; so WordPress does not try to load a page.
function my_plugin_action() {
if ( !isset($_GET['my-plugin-action']) ) return;
echo 'Run cron task here.';
exit;
}
add_action( 'init', 'my_plugin_action' );
To recap: Don't call your plugin directly. Make a URL that displays your content, then exit the script before WordPress tries to display the default page.

There is no such restriction by default in WordPress. You can access a file directly in WordPress if you want to.
See if the file code contains something like below -
if ( ! defined( 'ABSPATH' ) )
//some action if accessed directly
Otherwise, check back the URL and make sure your path is correct.
One more thing, if you will access the plugin file directly and it contains any WordPress core function, it will give error since in that case, WordPress core doesn't get loaded.
However, there are some non recommended ways to load WordPress core.

Related

how to create public file in wp plugin to access it from other apps?

I want to write a plugin so that I can connect to it with an app bot, and store information inside the database.
Note that because I directly connect to php file inside this plugin, the public WordPress values like $wpdb are not known.
And how can i access to wp functions? such as get_option,...
thanks for your help.
Maybe this helps you
require_once( 'path to wordpress project root'. '/wp-load.php' );
// Set up the WordPress query.
wp();
For example if you want to create plugin with name - test
You need to create a directory with name 'test' inside plugin directory wp-content/plugins/test
Then you need to create file test.php - it is the main file of your plugin
Then you need to add at beginning of your test.php file after comments require wp-load if WordPress environment is not included (it is your case when you call your file directly)
For example, your plugin main file is wp-content/plugins/test/test.php starts with this snippet and before this snippet, you need to place your plugin metadata like name - description and so on. I recommend to download WordPress and see default plugins meta in wp-content/plugins/askimet/askimet.php and wp-content/plugins/hello.php.
if ( !defined('ABSPATH') ) {
/** Set up WordPress environment */
require_once( '../../wp-load.php' );
}
then you can use get_option() $wpdb and other wp functions.
wp() function in my previous answer need for initializing global wp query. It is not necessary. You can create your own wp query by using WP_Query class.
https://codex.wordpress.org/Class_Reference/WP_Query - there is description with examples how to use WP_Query.
Then You can write your plugin in OOP or functional style. You can write your plugin analogically to Askimet for example or with boilerplates. This is the question about plugin development. Hope this question helps you achieve your purpose.
https://wordpress.stackexchange.com/questions/85486/is-there-any-plugin-development-framework
Hope this help you

How to update robots.txt on pantheon.io - wordpress

How can I update robots.txt on the pantheon environment Live site?
I have tried the following option
1) Via FTP
2) via word press SEO >> tool
Do I need to follow any steps, as it's a word press instance
Nothing special. Two options here,
Create a robots.txt file locally. Add desired statements. Upload to Pantheon via SFTP or Git.
Pull down the existing robots.txt file from Pantheon, modify as necessary, and push back up via SFTP or Git.
In both cases, you need to keep in mind that Pantheon forces a Workflow. You have the Dev, Testing, and Live Servers. When you push, whether by Git or SFTP, you are essentially pushing to the Dev environment. Note that if you choose to use SFTP, you must have the Pantheon site in SFTP mode (not Git), and you should log into the Dev environment SFTP. From there, you must deploy up to the Live environment. You do this via the Pantheon Dashboard.
EDIT:
Since you are going the SFTP route, you will need to login via SFTP to the dev environment. Once logged in via SFTP, you will want to upload to the /code directory. This is the root directory for the WordPress installation. So you will have uploaded /code/robots.txt. Once you upload, you will need to return to the Pantheon Dashboard and commit your changes through Dev, Testing, and Production.
Hope this helps.
If you do not have any experience with PHP and or don't feel comfortable modifying your themes code for whatever reason the solution above should work perfectly.
Alternative PHP approach
If this is a site you are developing / maintaining and feel comfortable modifying the theme there is another approach that will save you time in the long run.
Filters to the Rescue!
If you are unfamiliar with hooks and filters within WordPress I'll defer you to either this article from Treehouse blogs or a quick google search. The hooks and filter system plays a fundamental part in how plugins like Yoast SEO function, allowing them to modify the output of the robots.txt file for example.
We can use this same robots_txt filter to modify the output of our sites robots.txt file without any external plugin or theme dependency. If you use git or svn to manage your theme or /wp-content/ directories this approach allows you to keep any modifications under version control.
The code below should live in your themes functions.php file or another included PHP file of your chosing.
<?php
function so_robots_txt_50725645( $output ) {
// User-agent: *
$output .= 'User-agent: *' . PHP_EOL;
$output .= 'Disallow: /wp-includes/' . PHP_EOL;
$output .= 'Disallow: /wp-content/uploads/' . PHP_EOL;
return $output;
}
// Hook in our filter function.
add_filter( 'robots_txt', 'so_robots_txt_50725645', 10, 1 );
?>
What's listed above is just an example, you could populate the $output variable with whatever content you wanted to appear on the robots.txt page. In this example we are appending new Disallow lines to the existing output via the .= operator.
After all operations have been completed we return the modified $output and go on our way, never to worry about migrating pesky robots.txt files ever again.

How to upload totally custom PHP/web files into WordPress?

I know PHP but not much WordPress, and I need to upload a collection of files into my client's WP site. I'm wanting something a little different from what is being asked in this SO post.
I need to upload a collection of totally custom files, including PHP, html, js, and css files. Ideally I'd love to upload a zip with my collection of web files, with folder structure maintained. These files have nothing to do with the WP site, in that they don't need to hook in to the menuing/plugins/themes, etc. They are standalone files with custom functionality, I just need to launch my "main" page when a certain WP button is clicked.
Is there a way to upload a zip file through the WP admin interface and have it extract my files into a safe/isolated location?
How will I know the absolute or relative URL to my main page, after this upload occurs?
If 1 and 2 above are impossible, what's the best approach to get my files up there, in some kind of isolated folder? (I really don't want to create/modify themes or plugins if possible, I just literally want to dump some files onto the server and then know how to access them via URL.)
Note: If I had access to FTP on my client's server, that would be the easiest approach by far, but that is not an option, and my only doorway is the WP admin interface. This is a hosted wordpress site (version 4.7), hosted by Wordpress.com, and it's the Business Plan.
EDIT:
Here is Zero Gravity Payment Module.php:
<?php
/**
* #package ZeroGravityPaymentModule
* #version 1.0
*/
/*
Plugin Name: Zero Gravity Payment Module
Plugin URI: https://www.zerogravpro.com/CCPayment/
Description: Advanced Authorize.Net credit card payment form built by Zero Gravity Programming.
Author: Nate Jensen
Version: 1.0
Author URI: https://www.zerogravpro.com/
Text Domain: hello-dolly
*/
?>
And here is installer.php:
<?php
$destinationDir = $_SERVER['DOCUMENT_ROOT'] . '/' . 'ZeroGravityPaymentModule';
mkdir($destinationDir);
$zip = new ZipArchive;
$zip->open('ZeroGravityPaymentModule.zip');
$zip->extractTo($destinationDir);
$zip->close();
?>
I was able to upload my "plugin", but now cannot seem to find my php files via absolute url. I have tried the following locations:
https://[actualdomain]/wp-content/plugins/Zero%20Gravity%20Payment%20Module/installer.php
https://wordpress.com/view/[actualdomain]/wp-content/plugins/Zero%20Gravity%20Payment%20Module/installer.php
I have tested this answer end to end and it works. However, it now transpires that this question relates to Wordpress.com which provides modified/restricted versions of Wordpress - see edit at bottom of answer.
It can all be done with 2 miniscule scripts.
i: "mydummy.php" contains just a single comment on its own line (necessary to "fool" WP into uploading your actual script and archive file).
<?php
/* Plugin Name: mydummy */
?>
ii: "myinstaller.php" e.g.
<?php
$destinationDir = $_SERVER['DOCUMENT_ROOT'] . '/' . 'myparentdir';
mkdir( $destinationDir, 0775); // 0755 whatever
$zip = new ZipArchive;
$zip->open('myarchive.zip');
$zip->extractTo($destinationDir); // creates dirs and files below as per archive structure
$zip->close();
?>
Tested and works. Add your own error handling & info/progress msgs as required (you are familiar with php).
To upload and run:
Create a zip (e.g. myarchive.zip) of the directories and contained files you want to place on your server.
Place this zip and the 2 script files above in a folder called "mydummy".
Zip the "mydummy" folder" (the result should be "mydummy.zip" containing folder "mydummy" with your archive, myinstaller.php & dummy.php in it.
upload mydummy.zip via Wordpress admin: Plugins->Add New->Upload File. Dont bother activating. Your script and archive are now uploaded to (/maybe-path-to) /wp-content/plugins/mydummy.
run your script http://example.com/wp-content/plugins/mydummy/myinstaller.php JOB DONE
delete plugin, scripts and archive: via Wordpress admin: Plugins find "mydummy" and click its delete link
File perms shouldn't be a problem (I assume server will ensure files are 644 by default etc) the owner will obviously be the same as Wordpress - this should not be a problem for browser access.
Edit:
A common response to Wordpress.com questions is to advise contacting their support; and on a paid plan I would hope you get better answers from them than here. My knowledge of this host is near zero - I vaguely recollect its sites use a restricted variant of Wordpress and that on some(?) plans only specified plugins can be used.
You will need to build it as a WordPress plugin. Otherwise, it won't
work inside WordPress.com's structure.
If they mean the upload will only take place if there is a functioning plugin then try replacing the empty dummy plugin in my first answer with this:
<?php
/* Plugin Name: mydummy
Plugin URI: http://example.com
Description: do nothing plugin
Author: me
Version: 1.0.0
Author URI: http://example.com/
*/
$donothing = 0;
?>:
However; if they mean scripts will only execute if called by "Wordpress" you need to add extract code to plugin but ensure extract only occurs once not every time someone visits the site. In plugin, replace $donothing = 0 with:
function site_perm_function() {
// your code to create html string of dirs (e.g. root and current) + their permissions
// return permission string
}
add_shortcode( 'display_my_site_perms', 'site_perm_function' );
function install_function() {
// your code from myinstaller.php above with code to build message string from statement errors/results
// return message string
}
add_shortcode( 'myinstaller', 'install_function' );
Create a new zip and try installing and ACTIVATING plugin and. If OK:
Open the post editor to create a dummy post.
In the post editor insert the shortcode [display_my_site_perms] then preview the post - this should display permissions.
If permissions look OK for unarchiving then add shortcode [myinstaller] to the post and preview again - this should do the install.
Hopefully it will work, but not tested and I have zero knowledge of Wordpress.com.

How to update get_stylesheet_directory_uri in wordpresss

How do I update get_stylesheet_directory_uri? Transferring a website and I have already updated the url for the site inside wp-config using:
define('WP_HOME','https://somewebsite.com');
define('WP_SITEURL','https://somewebsite.com');
Still when get_stylesheet_directory_uri is called it is using a previous url, any idea on how to force this to update?
Thanks!
Once you change the site URL using those DEFINE statements, you also need to flush the permalink settings. Just go to wp-admin -> Settings -> Permalinks, and save without making any changes. That should force it to use your new values.
If you have access to the site via FTP, then this method will help you quickly get a site back up and running, if you changed those values incorrectly.
FTP to the site, and get a copy of the active theme's functions.php file. You're going to edit it in a simple text editor and upload it back to the site.
Add these two lines to the file.
update_option( 'siteurl', 'http://example.com' );
update_option( 'home', 'http://example.com' );
Use your own URL instead of example.com, obviously.
Upload the file back to your site, in the same location. FileZilla offers a handy "edit file" function to do all of the above rapidly; if you can use that, do so.
Load the login or admin page a couple of times. The site should come back up.
Another thing that can happen if your site is multilingual is that get_stylesheet_directory_uri is used in a registered string whose translation is still using the previous domain. In that case, search and replace your translations with the new domain.

How can I get access to Wordpress user functions

I am writing a small php file that is called via ajax from a plugin's js file to update a user's role. The file looks like this...
include "../../../../wp-includes/pluggable.php";
global $current_user;
get_currentuserinfo();
$u = new WP_User( $current_user->ID );
$u->add_role( 'trainer3' );
pluggable is the module that contains the get_currentuser() function. But I am getting additional errors (is_ssl() not found). What other Wordpress modules do I need to include to gain access to the current user object? The plugin I am using is not one that I wrote. It appears as though they did not include any Wordpress hooks. This code works fine in other plugins such as woocommerce.
Thanks!
Include wp-load.php instead or, preferably, use the Wordpress ajax actions
http://codex.wordpress.org/AJAX_in_Plugins

Resources