3

I am trying to create a custom WordPress plugin and I created a new folder inside the "plugins" folder. It's called "wp-services-table". Inside this folder I created two files: wp-services-table.php and wp-services-table-shortcode.php

For now the wp-services-table-shortcode.php file is blank. I only wrote this in the wp-services-table.php file:

<?php
/**
* Plugin Name: Table and Modal Window for Displaying Services
* Description: Displays Services in a table, by Categories and opens a Modal Window when the user clicks for more information.
* Version: 0.1.0
* Author: Ami
*/

//Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit; 
}

require_once ( plugin_dir_path(__FILE__) . 'wp-services-table-shortcode.php' );

But I get this error:

Fatal error: Call to undefined function plugin_dir_path() in /home/mysitedomain/public_html/wpfoldername/contentfolder/plugins/wp-services-table/wp-services-table.php on line 14

Could this be because I have a custom name instead of "wp-content"? Or because I installed WordPress in a subfolder ("wpfoldername") and not directly in the "public_html" folder?

Because oterwise I think that the what I wrote in the plugin's file is correct for creating a new plugin.

I also tried to add this:

$dir = plugin_dir_path(__FILE__);
var_dump($dir);
die();

To see what path it would show for the plugin, but of course I still got the same error regarding the undefined function plugin_dir_path()

I hope someone knows why this is happening because I'm very puzzled here. Thank you!

I edited the question to add the contents of wp-config: it might be useful because I have changed the name of the wp-content folder and also WordPress is installed in a subfolder. I think this is the part that is relevant:

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

/** Renaming the wp-content folder. */
define ('WP_CONTENT_FOLDERNAME', 'contentfolder');
define ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME) ;

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wpfoldername/');
define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Also the index.php file is in the "wpfoldername" folder and it contains the following code:

<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/

/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
  • What does $dir = plugin_dir_path( __FILE__ ); echo $dir give you ? – codiiv Feb 27 '17 at 13:37
  • @codiiv : Unfortunately it gives me the same error: Fatal error: Call to undefined function plugin_dir_path() in /home/mysitedomain/public_html/wpfoldername/contentfolder/plugins/wp-services-table/wp-services-table.php on line 16 – Ami Feb 28 '17 at 0:57
  • create a fresh installation of WordPress & check the plugin in that fresh installation. May be your WordPress installation is corrupted or some plugin is creating the issue. – Fayaz Feb 28 '17 at 1:16
  • Also how did you define contentfolder? – Fayaz Feb 28 '17 at 1:19
  • 1
    You shouldn't have to define ABSPATH within plugin, if that works, that means you have problem somewhere. Please edit your question and include the entire content of wp-config.php (excluding database info and keys and salts) + the content of the index.php file of the web root. – Fayaz Feb 28 '17 at 21:16
1

How do you access the page? Are you trying to access the pluginfile directly without using the frontend of the site? If that's the case Wordpress probably hasn't been loaded.

What happens if you add the following code at the top of your page?

if ( !defined('ABSPATH') ) {
    //If wordpress isn't loaded load it up.
    $path = $_SERVER['DOCUMENT_ROOT'];
    include_once $path . '/wp-load.php';
}

The code above makes sure Wordpress is loaded before your code runs.

  • Hi! Thank you for your answer! I tried your code - just that I added wpfoldername before wp-load.php like this: include_once $path . '/wpfoldername/wp-load.php'; - and it seems to be working and not showing that error again! But do I need to keep this code in the plugin's file? And if so, what do I do with the code: //Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } ? – Ami Feb 28 '17 at 20:21
  • You can remove the second line with exit;. That line is to deny access to the file directly without loading Wordpress. But since you want to access the file directly you need the lines I've given you. If the only way the file will be used is via Wordpress (using the frontend of your site), in that case use the rule with the exit; line. This is to prevent unauthorized access to the file. – Sjoerd van Hout Mar 1 '17 at 9:33
3

Could it be that you forgot to comment the Exit if accessed directly part?

//Exit if accessed directly

if ( ! defined( 'ABSPATH' ) ) {
exit; 
}
  • Hi!, Thank you for answering me! The line "Exit if accessed directly" is actually commented out in my file, I double-checked it. I accidentally deleted the comment signs when I copied it here. So that's not the cause. I edited the code in my initial question and commented out that line to avoid confusion regarding that. – Ami Feb 28 '17 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.