0

Firstly, I know this is 'bad' practice for several different reasons, but for reasons that I won't waste your time with it's something I need to do.

Using a fairly simple plugin and I've tried copying it into functions.php and updating the file paths and dependences, but I'm having some trouble.

Plugin file looks like this:-

$plugin_headers = get_file_data( __FILE__, array( 'Version' => 'Version', 'Name' => 'Plugin Name' ) );


/**
 * We store our plugin data in the following global array.
 * $my_unique_name with your unique name
 */
global $my_unique_name;
$my_unique_name = array();
$my_unique_name['version_key'] = strtolower( str_replace( ' ', '_', $plugin_headers['Name'] ) ) . '_version';
$my_unique_name['version_value'] = $plugin_headers['Version'];


/**
 * When the user activates the plugin we add the version number to the
 * options table as "my_plugin_name_version" only if this is a newer version.
 */
function inline_comments_acitvation(){

    global $my_unique_name;

    if ( get_option( $my_unique_name['version_key'] ) && get_option( $my_unique_name['version_key'] ) > $my_unique_name['version_value'] )
        return;

    update_option( $my_unique_name['version_key'], $my_unique_name['version_value'] );

}
register_activation_hook( __FILE__, 'inline_comments_acitvation' );


/**
 * Delete our version number from the database when the plugin is activated.
 */
function inline_comments_deactivate(){
    global $my_unique_name;
    delete_option( $my_unique_name['version_key'] );
}
register_deactivation_hook( __FILE__, 'inline_comments_deactivate' );


if ( is_admin() )
    require_once plugin_dir_path( __FILE__ ) . 'admin/admin-tags.php';

/**
 * Theme only functions
 */
require_once plugin_dir_path( __FILE__ ) . 'inc/template-tags.php';


function inline_comments_enqueue_scripts(){

    $plugin_headers = get_file_data( __FILE__, array( 'Version' => 'Version', 'Name' => 'Original Plugin Name' ) );
    $clean_name = strtolower( str_replace( ' ', '-', $plugin_headers['Name'] ) );

    wp_register_style( $clean_name . '-style', plugin_dir_url( __FILE__ ) . 'inc/css/style.css' );
    wp_register_script( 'textarea_auto_expand-script', plugin_dir_url( __FILE__ ) . 'vendor/textarea-auto-expand/jquery.textarea_auto_expand.js' );
    wp_register_script( $clean_name . '-script', plugin_dir_url( __FILE__ ) . 'inc/js/script.js', array('jquery', 'textarea_auto_expand-script') );
}

add_action('wp_enqueue_scripts', 'inline_comments_enqueue_scripts', 2);

After moving the plugin to the theme folder I've done the following: I've removed the pointless parts and in my functions.php I'm loading the main script.js (it loads) and the css, like so.

function inline_comments_enqueue_scripts(){
if ( is_singular() || is_page() ) {

    wp_enqueue_style( 'inline-style', get_template_directory_uri() . '/css/inline-style.css', '10000', 'all' );

    wp_enqueue_script( 'inline-script', get_template_directory_uri() . '/js/inline-script.js', array( 'jquery' ), MEDIUM_VERSION);
}   

}

add_action('wp_enqueue_scripts', 'inline_comments_enqueue_scripts', 2);

Ok, so our script loads with our css.

The problem is the main part of the plugin that contains the templates, the functions and the ajax calls I can't get to load.

This part:- require_once plugin_dir_path( __FILE__ ) . 'inc/template-tags.php';

I've tried pasting this file into functions.php and nothing happens, it just doesn't work.

Probably making a really stupid mistake somewhere(other than building a plugin into a theme, heh).

  • one thing you could do is create a folder called inc in your theme's directory, and inside it, add the file template-tags.php. The problem is that if I remember correctly, register_deactivation_hook need the file to be inside the plugins directory. – Tomás Cot Sep 13 '14 at 23:21
  • So an inc folder in my plugin directory wouldn't work with that hook? Or is there another hook? I've tried just pasting template-tags.php into my functions php, no bueno. – andy Sep 13 '14 at 23:27
  • Are you sure you don't need the rest all the code? You just need whats below the "Theme only functions"? – Tomás Cot Sep 13 '14 at 23:39
  • Yup, pretty sure, what else is there? – andy Sep 14 '14 at 0:14
  • The template-tags.php file, that doesn't seem to be used, but it could have the part where the AJAX action is registered, ad that is key if you are saying this plugin uses AJAX calls. – Tomás Cot Sep 14 '14 at 1:23
1

Just because you think that you have to do it doesn't remove the fact that it is very not smart to do it. The plugin code is built to be used as a plugin and not as a theme, and while you can copy parts of the code and use them in the theme there can not be any simple way to make sure there is no dependency left somewhere in the code.

The easiest way to do this kind of things is to check that a plugin is active and force its activation if it isn't (and you can't trust your client not to deactivate it).

  • if the plugin is required, put it in a mu-plugins folder inside of wp-content. – Welcher Sep 17 '14 at 21:17
0

I'm making some brief assumption based on the way you have typed your question..

"After moving the plugin to the theme folder"

Meaning you have taken the plugin from the plugin directory to your wp-content/themes/THEME_FOLDER. If this is the case then calling plugin_dir_path( __FILE__ ) will be unsuccessful in finding your file.

You would need to use get_template_directory_uri() or something else.

Please review the Codex to better understand the function.

If I am incorrect in my assumptions please let me know as I may be able to correctly address the issue with more information.

  • meh I keep changing my mind either use get_template_directory_uri() or bloginfo('template_directory') both are fine enough in my book. I don't have problems either way. – Cayce K Sep 14 '14 at 1:40
  • As you can see, the first part of my question is the raw plugin and I changed the code in my functions.php to load the scripts from their folders. I just don't know how to get the vital part, template-tags.php to load, even pasting it into my functions.php doesn't work. – andy Sep 14 '14 at 9:53
  • Ok... Did you try the bloginfo or get_template_directory though? I'm not sure if I fully understand. The most likely situations is that the file you are attempting to target is not in its correct location or expected directory. It might be more wise if you made a duplicate of the file and did not edit the original. Then put the duplicate in your themes folder maybe in a directory or something and reference from there. You can use the get_template(..) function and your original plugin will be less likely to cause any major issues. – Cayce K Sep 14 '14 at 21:19
  • I already made a duplicate, put the relevant scripts etc into my theme folder, I'm just having trouble getting template-tags.php to load and console gives me 'ajaxurl is not defined' as a result. Here's the plugins' git if you're interested github.com/pommiegranit/paragraph_commenting – andy Sep 15 '14 at 10:06
  • OK that should help me a lot more. Sadly I can't look at this until 5pm EST so give me a bit. HOPEFULLY you solve this before I get to it so you don't have to wait that long. I think I have ran into this once before, but I can't remember the steps I took off top my head so when I can I will get to you asap!! – Cayce K Sep 15 '14 at 12:09

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.