I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.
Related
I am going to write a super small cms with https://github.com/panique/mini/
Now I want to add a small pages section in the Admin of the site (this can be done easily ).
The advice part comes here:
The url of the mini framework is mostly easy, its /controller/method ( if its the index method then it won't needed to be shown in the url ).
So there is a file which checks if the controller is existing so it can load it.
But the thing is an user is not going to create a controller every time after creating a page.
What would be the best approach to do this ?
This file is checking if that controller exist: https://github.com/panique/mini/blob/master/application/core/application.php
Thanks in advance
The way this handles routing, is much like CodeIgniter. The path would be controller/method/arg1/arg2/arg3..., so you can write a controller, define a method which accepts one (or more) arguments to load user pages.
Assume, user has created a page named news. He/She may load the page via URL pages/view/news.
So here is my situation:
I got a plugin directory that all needs the same functions. So i wrote a general.php with the common functions that is used by every plugin. Now i could let the developer of the plugin include my general.php file but this could be very annoying for him/her since its very nested so when a developer wants to include it he/she gets this: include '../../../../../../../lib/general.php' So what i did is let the plugin loader include the general file beforehand so that the developer wont need to access the general file every time it has a new file. Now this works and all until the developer does a form and needs to get POST data.
But i can't access the POST data inside the include, is there a way to access the POST data from the include file?
The loading kinda goes like this:
include '../lib/general.php';
include Plugin::GetPluginViewPath;
i can't access the POST data inside the include,
You can.
is there a way to access the POST data from the include file?
Yes. Just access it.
As long as $_POST array is populated and not unset by some code, it is perfectly accessible everywhere.
Of course you should include PHP files, not HTTP resources, though
I'm trying to build a simple PHP MVC framework. What I have right now is a basic set-up with a router that sends the request to the right controller, so I'm making the URL
/blog/[id]
Resolve to a class method like
Blog->singlePost();
Within singlePost(); I then interact with the model to get the right blog post and send it off to the view. The basic MVC triad is all there.
But pages are more complex than this. I've got a sidebar that I need to fill with data (from the database and other sources). I've got a main menu to build, I need to show the details for the current logged in user, and more. But, that said, I don't need all of these sections on every page.
My question is, where do I initiate/build all these various shared sections for the view?
My options that I can think of are:
Have every controller send a request for the required additional sections to be built (would have to update every controller each time something is added, no way)
When mapping the URL's, define a group that the route belongs to that loads in all the required sections (Not ideal, not every section will be needed on every page, could get messy)
Have the View (or Template) embed these sections on-demand. So the data for these sections would not be retrieved until the view explicitly asks for the data (The basic functionality of this seems quite appealing, but does it break the rules of what the view should do?)
Something else?
3 Seems quite appealing to me because I'd like for my templates (that will be dynamic and switchable via a template system) to be able to request a section on any page. So one template may show the "Top 10 Blog Posts" on the homepage, but another may not, so there's no point in loading it. I see that with Symfony/Twig, you can do this: http://symfony.com/doc/2.0/book/templating.html#embedding-controllers
Is this a good idea for various modules on a site? Or should everything be loaded before the view starts being processed?
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
I have a plugin that imports csv data into wordpress tables. It runs through the Wordpress dashboard where you input some details and click a button to execute it.
I've altered the plugin so that the input data is static(from the same csv every time) and is now all located in one php file. I want to schedule a Cron job to execute this script every hour or so.
I tried to set it up using cPanel and directly accessing the php file but it does not work(nothing is displayed). I believe this is because the plugin uses wordpress functions such as wp_insert_post.
How can i run this script, as if it were run through wordpress dashboard, as a scheduled event?
Note: the file also contains some javascript.
You’ve got to include 2 files to get access to admin side functions: First, wp-load.php. wp-load.php gets everything set up, and fires up wordpress. However, you are calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) – so when wp-load is called, you are not going to be in the admin section, and not going to get access to those functions. On the bright side, you also don’t have to deal with WordPress forcing you to login. Since you still need those admin functions, include wp-admin/admin-functions.php. This loads up the admin side and gives you access to the admin functions – and you are set to go
You should use wp_schedule_event. See in WordPress codex here : http://codex.wordpress.org/Function_Reference/wp_schedule_event. Use something like this:
register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');
function my_activation() {
wp_schedule_event( time(), 'hourly', 'my_hourly_event');
}
function do_this_hourly() {
// do something every hour
}
You can perfectly call a PHP file inside your do_this_hourly() function.
Accepted answer worked well for me. I modded this, and here are my findings. It is the case that you can use this solution outside of a plugin scenario. Basically, you can have your server run using cron jobs using native wordpress functionality as followed. You can create a file at the top level of your wordpress app and then include /wp-load.php; therein.
This loads in native wordpress and allows you to call a class that you can define as part of your must use plugins. Then your good to go regarding basic wordpress functionality, things like $wpdb and get_usermeta()
Now you can instantiate your class with $class_variable = new YourClass; and from there you can call class functions. The great part about this is that you can schedule cron jobs as you would normally on your server, and when you do you have your wordpress function running as it would inside wordpress. This means you don't need to maintain your cron job stuff as part of a plugin, which may or may not be useful depending on what your up to.