Wordpress - How to include() a file that lies in another dir - wordpress

I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).
My file lies in /plugins/new-plugin/new-plugin.php
and I need to make a
include(/plugins/OLD_plugin/old-plugin.php)
so I can use a couple of functions from the old-plugin.php file.
What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.
I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.
I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.
With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other solutions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.
The new-plugin.php is as simple as
<?php
/*
WP Common Headers
*/
global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';
add_action('admin_menu', 'new_plugin_menu');
function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';
add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}
function new_plugin_admin_page() {
$result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");
$total = count($result);
old_plugin_list_links($result, $total, FALSE, FALSE);
*/
}
?>
thanks for any ideas!

check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.
see http://codex.wordpress.org/Function_Reference/do_action
and http://codex.wordpress.org/Function_Reference/apply_filters
For example (very basic example):
If in old plugin you find a:
do_action('some_type_of_reference);`
In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');
function name_of_my_function() {
//executed code here
}`
If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);
Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');
function my_function( $variable ) {
//act on the variable from the filter.
return $variable;
}

Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.
The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.
Hope this helps!

Related

WordPress and a custom folder for page-templates

I want to add my custom page-templates for Wordpress in a custom named folder (eg. not 'page-templates').
Although following code works, for ALL page-templates, not just homepage, this code doesn't make sense to me?
function custom_page_template_directory( $page_template ){
$BASE = get_stylesheet_directory() .'/template-pages';
if ( is_page( 'homepage' ) ) {
$page_template = $BASE . '/page-homepage.php';
}
return $page_template;
}
something like this would make more sense, although it's offcourse fictional. Does anyone know how to actually do this?
function custom_page_template_directory( $page_template ){
$BASE = get_stylesheet_directory() .'/template-pages';
if ( file_exists( $BASE . post -> post-template ) ) {
$page_template = $BASE . post -> post-template;
}
return $page_template;
}
Note: this is needed for page-templates only, I've already got a nice solution for single-post-templates
it is not necessary to do this. Wordpress recognizes theme files by name. For example, if you want to make the home template, you just have to create a file called home.php.
if you need to list posts: archive.php and if you have a working post type: archive-posttypeslug.php
In the case of pages it is different, you have the page.php and if you want a custom, you have to generate the file and then assign it from the wordpress backend (in the edition of that page).
Here is some information on this topic, I hope it helps you.
https://codex.wordpress.org/Theme_Development
https://developer.wordpress.org/themes/basics/template-hierarchy/

is wrong to add my code in wp admin page wordpress

I must to add new options and functions in post pages in admin panel. I call a new function in edit-form-advanced.php and edded this function in template.php file. The question is this wrong? Becouse my function is in one file with functions on wordpress. Or maybe must be in other file? but where i must call it?
For wp-content part i know and i make a child theme of parent theme, but i do not know what to do when i must add code in wp-admin part.
example:
edit-form-advanced.php
do_custom_boxes( null, $post );
and in template.php
function do_custom_boxes( $screen, $object ) {
global $wpdb;
$appTable = $wpdb->prefix . "post_panel";
$query = $wpdb->prepare("SELECT * FROM $appTable WHERE post_id = ".$_GET['post']." ", $screen);
$applications = $wpdb->get_results($query);
......
}
Short answer: Yes, it's wrong to do so. Whenever you update your WordPress you'll loose all your changes.
WordPress allows you to hook into its code, modify its behavior and many things.
Please read about actions and filters.
Basically, Actions allow you to fire a function when something happens in WordPress.
For example:
<?php
function do_something_when_admin_pages_init() {
// Do something here
}
add_action('admin_init', 'do_something_when_admin_pages_init')
Filters allow you to modify data/output of another function. It's like it let you step in the middle, do something with the data and then continue.
Example from the WordPress page:
<?php
function wporg_filter_title($title) {
return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');
This modifies the title before it's printed.
So with those two ways of 'hooking' into the WordPress code, you can write your code in your theme's functions.php file, or write a Plugin (it's up to you).

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

hijack get_template_part via plugin

I'm trying to do a plugin that will change the behavior of a theme.
In the theme file I have a get_template_part('libs/templates/user_menu');
I want to make my plugin to "force" the get_template_part return another slug file (a path to a file in plugin folder).
So far this is my code inside the plugin:
function wpse21352_template_part_cb( $slug )
{
if(slug == 'user_menu') {
return WP_PLUGIN_URL.'/'.$slug;
} else {
return $slug;
}
}
do_action( "get_template_part_user_menu", 'user_menu' );
add_action( 'wpse21352_template_part_cb', 'get_template_part_user_menu', 10, 1 );
First of all, get_template_part does not return anything. It loads a file from your theme based on the parameters you pass to it. The function does not support filtering, which means you can not actually overwrite what is outputted by get_template_part.
The only thing the action get_template_part_[slug] allows you to do is output something before the theme file is loaded. For example, using
function myplugin_before_login( $slug, $name ) {
echo 'Example';
}
add_action( 'get_template_part_login', 'myplugin_before_login', 10, 2 );
would output "Example" before the loading the theme file when calling get_template_part( 'login' );.
Actions and filters
In general, however, I believe you might misunderstand how actions and filters work. The WordPress Codex offers extensive information on their use and usage.

Wordpress: Plugin for simple user profile pages

In WordPress, I am aware that tld.com/author/username exists for authors, but I am looking for a public user profile page for non-authors. I want to setup a simplistic "favorite's list" for members on my site. Users will create an account, and add posts they like. They don't need access to wp-admin.
I'm looking for something simple like tld.com/user/username -- not /user/?uid=1. Nice and "pretty". Just like how WordPress handles /author/admin, or /author/username.
I would also like to keep /authors preserved so that's accessible too.
I have tried many plugins like WordPress-Users, but it's not a "pretty" URL, also have tried complicated plugins like Members, profile-builder, wp-user-frontend.
I found the answer to this from #bybloggers answer found here. https://wordpress.stackexchange.com/a/58793/12920
I modified his code very slightly to tailor it to my needs, but this is the code that worked for me and was exactly what I was looking for:
// Create the query var so that WP catches the custom /member/username url
function userpage_rewrite_add_var( $vars ) {
$vars[] = 'member';
return $vars;
}
add_filter( 'query_vars', 'userpage_rewrite_add_var' );
// Create the rewrites
function userpage_rewrite_rule() {
add_rewrite_tag( '%member%', '([^&]+)' );
add_rewrite_rule(
'^member/([^/]*)/?',
'index.php?member=$matches[1]',
'top'
);
}
add_action('init','userpage_rewrite_rule');
// Catch the URL and redirect it to a template file
function userpage_rewrite_catch() {
global $wp_query;
if ( array_key_exists( 'member', $wp_query->query_vars ) ) {
include (TEMPLATEPATH . '/user-profile.php');
exit;
}
}
add_action( 'template_redirect', 'userpage_rewrite_catch' );
After this was in my functions.php file, I had to re-save my Permalinks.
Sometimes re-saving the permalinks didn't finish the job 100% and browsing to www.mysite.com/member/username would 404, so I had to manually flush the rules by putting this into my functions.php and loading my site once. Then removing it so I don't run it every time the site loads, since that's unnecessary overhead.
// Code needed to finish the member page setup
function memberpage_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','author_rewrite');
I don't know if you will find this one, at least not for free. Have you checked out WPMU? I started writing a membership plugin a few months ago but never completed it and am now doing it in Symfony. Most WordPress membership plugins are either too complex to use or don't provide the features you need.
You should spec out what you need an get a local dveloper to build it for you, you might even be able to sell it if you do a good job.

Resources