WordPress plugin_dir_path now working

Ask any WP Development Related Questions here
Post Reply
Lisa
Posts: 2
Joined: Tue Oct 11, 2022 4:35 pm
2

WordPress plugin_dir_path now working

Post by Lisa »

Why WordPress plugin_dir_path now connecting my JS and CSS Files.

I am learning WP plugin development from a YouTube tutorial. The code is working in the video but the person is using local host environment. Whilst the code is not working on my side. I am using live hosting environment.
The plugin_dir_path is connecting the main plugin file and other php files added with require() function but it is throwing critical error when I try to use plugin_dir_path() to link the js and css files. Any Idea what is the issue :?:
User avatar
HiraAnsari
Site Admin
Posts: 3
Joined: Tue Oct 11, 2022 4:26 pm
2
Contact:

Re: WordPress plugin_dir_path now working

Post by HiraAnsari »

if plugin_dir_path(); is working fine to connect the main plugin file then keep using it to define the plugin directory path. But for the JS and CSS files use plugin_dir_url( ); instead.

:idea: Example:

Code: Select all

// Check and Define Directory Path

if ( ! defined( 'PREFIX_PLUGIN_DIR' ) ) {
	
	define( 'PREFIX_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}


if ( !function_exists( 'prefix_plugin_scripts' )) {
	
		function prefix_plugin_scripts(){

        wp_enqueue_script('plugin-name-or-prefix', plugin_dir_url( __FILE__ ) . '/assets/js/plugin-name-or-prefix-main.js', '1.0.0', 'true' );

	}
}

add_action( 'wp_enqueue_scripts', 'prefix_plugin_scripts' );		

Hira Ansari
WP Developer and SEO Manager
Design Develop SEO London
Post Reply