wordpress plugin : Call to undefined function add_menu_page() - php

I am working on a wordpress plugin and i decided to use OOP instead of functional programming,however;I am receiving this weird error:
Error Message:
Call to undefined function add_menu_page() while am pretty sure everything is working as intended
The Code:
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
and i initiate the code with $admin = new bdmin();
Best Regards

This should remove the error:
<?php
if(!empty($value['id'])) {
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
}
?>

To solve this problem all I needed to do is invoke a function named add_action with these parameters.
add_action('admin_menu', array( &$this , 'create_admin_menu'));

Make sure you include the header file at the top so you can make WP function calls
<?php require('{correct_path}/wp-blog-header.php');

Related

register_activation_hook is not working - Wordpress

I'm trying to create a plugin for shortcodes. But my activation hook is not working. Plugin is activated in my wp-admin/plugins page but nothing works which is in my code like:
My enqueue & shortcode functions are not working. I've tried plugin_dir_path(__FILE__) & plugin_dir_url(__FILE__) b ut nothing works.
Here is my code:
if (!class_exists( 'DiliviBlocks' )) {
class DiliviBlocks {
public function __construct() {
$this->setup_actions();
$this->plugin_dir = plugin_dir_path(__FILE__);
}
/**
* Setting up Hooks
*/
public function setup_actions() {
register_activation_hook( plugin_dir_path(__FILE__), array( $this, 'activate_plugin' ) );
}
/**
* Activate callback
*/
public function activate_plugin() {
add_action( 'wp_enqueue_scripts', array($this, 'dilivi_blocks_scripts') );
add_shortcode( 'vans_search_form', array($this, 'vans_search_form_shortcode') );
}
public function dilivi_blocks_scripts() {
wp_enqueue_style( 'dilivi-blocks-plugin-css', plugin_dir_path(__FILE__) . '/assets/css/styles.css');
wp_enqueue_script( 'dilivi-blocks-plugin-jquery', plugin_dir_path(__FILE__) . '/assets/js/jquery.js' );
wp_enqueue_script( 'dilivi-blocks-plugin-js', plugin_dir_path(__FILE__) . '/assets/js/scripts.js' );
}
public function vans_search_form_shortcode($atts) {
return 'Hello World';
}
}
$diliviBlocks = new DiliviBlocks();
}
Please help me. I'm stuck
The documentation states that you only need the plugin directory name, and the name of the initial file. You should not include the whole path to it.
If your plugin contains only the file, than just __FILE__ would suffice.
In your case, it will probably be something like dilivi-blocks/divili-blocks.php.
Also, be aware that functions will only be executed once -- when your plugin is activated. If it is already active, the functions will not execute.
You should know that enqueueing stylesheets and the like will have to be done every time a page loads, so binding them to a single-use hook will not work. The wp_enqueue_scripts hook should be used for that.

Wordpress Plugin Boilerplate & Submenu Priority

I wanted to create a submenu but setting the priority to 11 doesn't seem to work.
I have this on my class-plugin-name.php >> define_admin_hooks()
$plugin_admin = new Plugin_Name_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_menu' , $plugin_admin, 'make_menu' );
And on my Plugin_Name_Admin class, I've got:
public function make_menu() {
add_submenu_page('wp-plugin-name', 'Test', 'Test', 'manage_options', 'wp-plugin-name-test', array($this, 'test_page') );
}
public function safe_page() {
echo "test";
}
My problem is that the submenu item shows up but gives me an Insufficient Permissions wp_die(); error.
Now I did a lot of google search and I found out you need to put 11 on the priority instead of 10 like so:
$this->loader->add_action( 'admin_menu' , $plugin_admin, 'make_menu', 11 );
But it doesn't work and the submenu item won't show up instead. I tried putting everything on the wo-plugin-name.php on the root file and just placed simple:
add_action('admin_menu', 'wp_backitup_safemenu', 11 );
function wp_backitup_safemenu() {
add_submenu_page('wp-plugin-name', 'Test', 'Test', 'manage_options', 'manage_options', 'wp_plugin_name_menu_content' );
}
function wp_plugin_name_menu_content() {
echo '<h1>FOO</h1>';
}
And it works like a charm. So what did I do wrong? or is this a boiler plate loader problem?

How to add a custom post type in my OOP plugin?

I require to create a custom post type for a plugin. Well, I decided to create the plugin in an OOP way, so basically I used the WordPress-Plugin-Boilerplate by Devin Vinson as starter point.
I have seen many plugins which add the custom post type in the main plugin file like this:
add_action( 'init', 'create_my_custom_post_type' );
function create_my_custom_post_type(){
$labels = array( ... );
$args = array( ... );
register_post_type( 'my_custom_post_type', $args );
}
Now, since I am trying to do it the right way, instead of doing that, I went to the file class-plugin-name.php inside de /includes directory and created a new private function like this:
class Plugin_Name {
private function register_my_custom_post_type(){
$labels = array( ... );
$args = array( ... );
register_post_type( 'my_custom_post_type', $args );
}
public function __construct(){
// This was also added to my constructor
$this->register_my_custom_post_type();
}
}
Since this is run every time the plugin is called, I figured the post type would perfectly be created, but I am getting this error:
Fatal error: Call to a member function add_rewrite_tag() on a
non-object in /public_html/wordpress/wp-includes/rewrite.php on line
51
I am pretty sure the problem is my new function, anyone has any ideas on how to do it correctly? Maybe I should put the code outside that class and just create a new class and create a hook for init?
You're right about hooking into init to register you custom post type. Here's the correct syntax:
public function __construct() {
add_action( 'init', array( $this, 'register_my_custom_post_type' ) );
}

Why does my save_post action not trigger?

I'm writing a plug-in for wordpress in classes and I'm adding an action hook to 'save_post' in the constructor of a class. However it does not seem to fire. Am I using it in the right way?
EDIT 25-05-2014 - I wrote a new (tested) minimal example that definitely reproduces the problem for me.
If I use the save_post in a procedural way (like directly in the index.php) it does work, but obviously that's not helpful when I'm structuring everything in classes.
/*
File index.php
This file handles the installation and bootstrapping of the plugin
*/
define("XX_POST_TYPE", 'testposttype');
if( !class_exists('MyPlugin') ):
class MyPlugin {
var $savecontroller;
public function __construct(){
add_action('init', array($this, 'init'), 1);
//include stuff before activation of theme
$this->include_before_theme();
}
//Include these before loading theme
private function include_before_theme(){
include_once("controllers/savecontroller.php");
}
public function init(){
register_post_type( XX_POST_TYPE,
array(
'labels' => array(
'name' => __('Tests'),
'singular_name' => __('Test'),
'add_new' => __('Add new test'),
'add_new_item' => __('Add new test')
),
'public' => true,
'has_archive' => true,
'hierarchical' => true
)
);
add_action('add_meta_boxes', function(){
$this->savecontroller = new SaveController();
});
}
}
function startup(){
global $myPlugin;
if( !isset($myPlugin) ){
$myPlugin = new MyPlugin();
}
return $myPlugin;
}
//Initialize
startup();
endif;
?>
The save actions happen in a different class and file.
<?php
// file savecontroller.php
class SaveController{
public function __construct(){
add_meta_box('xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE);
}
public function setup_field( $post ){
?>
<input type="text" name="xx_custom_field" id="xx_custom_field" value="">
<?php
add_action('save_post', array($this, 'save_my_post'), 1, 1);
}
public function save_my_post($post_id){
if(isset($_POST['xx_custom_field'])){
update_post_meta($post_id, 'xx_custom_field', $_POST['xx_custom_field']);
}
}
}
?>
It does create my custom posttype and field so I know the classes are working. But the save_post is not triggered. It does not 'die()' and it does not do the 'update_post_meta()'. The custom field does appear in the POST request so the isset() checks out.
It's probably something dumb, but I can't get it to work.
You're trying to add the hook save_post inside the add_meta_box callback, and that's not the place for it.
To solve it, change the init method to
public function init(){
register_post_type( $args );
$this->savecontroller = new SaveController();
}
And modify the SaveController to
class SaveController{
public function __construct(){
add_action( 'add_meta_boxes', array( $this, 'meta_box' ) );
add_action( 'save_post', array( $this, 'save_my_post'), 10, 2 );
}
public function meta_box(){
add_meta_box( 'xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE );
}
public function setup_field( $post ){
?>
<input type="text" name="xx_custom_field" id="xx_custom_field" value="">
<?php
}
public function save_my_post( $post_id, $post_object ){
wp_die( '<pre>'. print_r( $post_object, true) . '</pre>' );
}
}
Note that the save_post action takes two parameters and the priority can be default (10). You can find lots of examples for meta boxes and save_post here.

How to define and use helper functions in theme functions.php?

How to get following to work properly in Wordress Theme functions.php file?
I haven't figured out how to make the top function available to the bottom function within the theme's functions.php file. I'm not grasping how to setup hooks so they can work together. Thank you.
Filter/helper/whateveryoucallit function:
function poohToPee( $pooh_log )
{
switch( $pooh_log )
{
case 'gross-poop':
$pee_equivalent = 'Grossest of Pees';
break;
case 'ok-poop':
$pee_equivalent = 'Bland Snack Pee';
break;
case 'shang-tsung-plop':
$pee-equivalent = 'Random U-Stream';
break;
}
return $pee_equivalent;
}
Ajax handler function:
function screw_loose()
{
if( isset($_REQUEST['pooh_log']) )
{
echo poohToPee( $_REQUEST['pooh_log'] );
}
}
add_action('wp_ajax_priv_screw_loose', 'screw_loose')
The add_action usually calls the function you are passing through at the point that hook is called.
Since you are using some sort of ajax hook are you really able to make sure your function isn't being called? It wouldn't be echo-ing anything out to the screen since it is running in the background.
Normally any function you define in functions.php is readily available to use within the theme.
It is obviously normally best to organize and have classes, in which case you'd pass the method to the hook as an array, for example add_action( 'admin_init', array( $this, 'someFunction' ) ); and that add_action I just did would be put in the __construct function of the class.
For instance you could do this:
class helloWorld
{
function __construct()
{
add_action( 'admin_init', array( $this, 'echoItOut' ) );
}
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
Alternatively you could also do this:
class helloWorld
{
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
add_action( 'admin_init', array( $helloWorld, 'echoItOut' ) );
Or simply:
function echoItOut()
{
echo 'Hello World';
}
add_action( 'admin_init', 'echoItOut' );
If you put any of these blocks of code I provided in your functions.php file and go to your Dashboard you will see 'Hello World' printed out at the top under the admin bar most likely (might vary from theme to theme if the dashboard has custom styling).

Categories