For some reason, I can't get working the WordPress function get_editable_roles(). Is there a bug with this? Doesn't mind where I add, in the functions.php, in the index.php, in the top, in the bottom. It always gives the undefined function error.
I added this line of code in Twenty Fifteen theme, in a fresh WordPress install and without any plugin:
$role = get_editable_roles();
It give this error:
Error: Call to undefined function get_editable_roles()...
If I load the user.php file before, then it works:
if (!function_exists('get_editable_roles')) {
require_once(ABSPATH . '/wp-admin/includes/user.php');
}
You should consider to load user.php even you are in admin side, especially you have created a custom admin page like options.
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$roles = get_editable_roles();
It is also discussed here.
I think the problem is that you're trying to use this function outside the admin section where it isn't loaded.
From the Wordpress documentation:
Notes
The file that defines this function (wp-admin/includes/user.php) is only loaded in the admin sections.
See: https://codex.wordpress.org/Function_Reference/get_editable_roles
Related
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).
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();
}
at the moment I am working on a Wordpress theme with a functions.php that will include multiple helper functions files. I want this theme to be very flexible and customizable if a child theme is made based on the theme.
I have this piece of code currently in my functions.php:
add_action( 'template_redirect', 'wpf_contact' );
/**
* Include the logic/settings for tpl-contact.php.
*
* Includes the logic/settings for tpl-contact.php. It will load the child's
* mail.inc.php first if that is available.
*/
if ( ! function_exists( 'wpf_contact' ) ) {
function wpf_contact() {
if ( is_page_template( 'tpl-contact.php' ) && is_child_theme() && file_exists( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' ) ) {
include( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' );
} else {
include( get_template_directory() . '/inc/wpf/mail.inc.php');
}
} // end wpf_contact()
}
What the code above does is load mail.inc.php from child if the files exist. If it doesn't load it from the parent theme.
Cause I am having multiple files I want to load like that. I was thinking it there might not be a easier way of doing this? Is there a built in function that does this in WordPress?
Or should I just adjust the above function and add parameter's so it can be used for other files as well? Not sure if this is efficient.
To answer your question directly based on your sample, i would not check to see if the file has been placed in the child theme. If a child-theme developer has customized the file, they can simply declare the function and include their own file (wherever they place it). There is no risk of code duplication because your sample function is simple enough.
Even if you simplified your code for the purposes of placing it here....you can always keep includes wrapped in a very simple function that can be declared by a child theme. This will keep PHP running efficient and not having to check for files all the time.
I do not know if I can ask word press templated questions here. I was trying to add a line to functions.php in order to display images in RSS feeds. However, I missed to add a "/ "at the end of my comment line and my website crashed.
I am getting this error:
Fatal error: Call to undefined function load_theme_textdomain() in /home/content/31/6570531/html/wp-includes/functions.php on line 33
the 33 line of functions.php has this line of code:
load_theme_textdomain( 'btp_theme', get_template_directory().'/languages' );
is there anyone who knows what should I do? I reupload template files but still getting the same error.
this is the part of error I think
/* Initialize translation mechanism */
load_theme_textdomain( 'btp_theme', get_template_directory().'/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable($locale_file) )
require_once($locale_file);
/* EnableWP Auto Feed Links */
add_theme_support('automatic-feed-links');
/* Enable post thumbnails support */
add_theme_support( 'post-thumbnails' );
/* Enable custom backgrounds support */
add_custom_background();
if ( ! isset( $content_width ) ) $content_width = 593;
I solved my problem and want to share it with you.
You should download a fresh copy of WordPress. Than delete
wp-config.php
and
wp-content directory
from your wordpress copy on your harddrive. These are vital steps in order you to not loose your settings and content.
Than upload the Wordpress files via FTP to your server.
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!