making plugin for auto commiting any file change in github - php

I am making plugin in wordpress site, which see if any changes in any file occured, if they did, then committing the whole codebase to github. I know how to use github api in php, but I need to know, how will I look at changes happening in the wordpress site. Is there wp hook or php code to do it.
I tried to look at the current theme's directory and scanning all the files with scan($files) function, and check for file modifcations with wp_check_file_modifications($files) plugin function, but it works for core wordpress files.
<!-- to get the current theme's directory -->
$theme_directory = get_template_directory();
<!-- to scan through all the files in current directory -->
$files = scandir($theme_directory);
echo 'All files in current theme Directory:' . "\n";
print_r($files);
if ( wp_check_file_modifications( $files ) ) {
At least one of the files has been modified
} else {
All of the files have not been modified
}
Is there any other way around it or any other idea with which I can approach this problem? Thanks in advance for help.

Related

Creating a separate php file for WooCommerce hooks

I can't seem to find any documentation on this so maybe someone here can help. I have been adding all my WooCommerce hooks to my functions.php file but I would rather have all of them in a separate file to keep things tidy. Obviously I will need another php file for these but how do I link it to the functions.php file so that the code runs as it does now? Thanks
just create new file with your woocommerce hooks code name anything what u want.
Ex. woocommercehook.php and place file in theme folder.
in function.php file just include your woocommercehook.php file at last.
include("woocommercehook.php");
Done. all work same as early.
The PHP include/include_once and require/require_once will work fine. However,
create another file with your hooks( woo-hooks.php ) and use WP get_template_directory to require the file.
require get_template_directory() . '/woo-hooks.php';

WooCommerce, how to load a custom javascript file while using Mystile theme?

Hi I have a WooCommerce plugin activated and a Mystile theme. I want to add a custom js file located at mystile/includes/js/custom-script.js. Ive read on how to add files through using wp_enque_script but it is not loading the js files when I view page source.
I have this code block on functions.php of my current theme which is Mystile, which should have added my custom js file
function add_custom_assets(){
//Load masonry CSS and JS files
wp_enqueue_script('masonry', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}
add_action('wp_enqueue_scripts','add_custom_assets');
the above code does not work and I cannot see my JS file in source. I really dont know what I am doing wrong. I even tried wp_register_scripts but its the same thing.
The only change I did to the current theme was that I copied the templates folder from the woocommerce plugin directory to my current theme and renamed it woocommerce to override certain stuffs. Any suggestions
you should specify unique handles (so stuff don't get overridden)
function add_custom_assets(){
wp_enqueue_script('my-custom-script-handle', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}
add_action('wp_enqueue_scripts','add_custom_assets');
This is not a ideal solution but a hack and it does work. Enqueue the script as wp_enqueue_style rather than wp_enqueue_script. It does load the script but is a very dirty solution. Still the question is open though

Woocommerce (shop) header in product-archives.php file

I read that it is possible to place alternative header into woocommerce pages using get_header('shop'); in product-archives.php template in my custom theme. In order to make it work I need to make copy of header.php file and rename it to header-shop.php. It should be also located in same folder as header.php file.
Unfortunately I can't get this to work.
Anything I am missing if somebody notices that would be great help.
You can have a work around.
Just make a copy of header.php in your current theme with the same name (i.e. header.php).
Create another copy of the header.php with you desired name(i.e. header-shop.php).
Now you just need to add a custom code in header.php at the starting lines to check that whether its a product archive page(using this function http://codex.wordpress.org/Function_Reference/is_post_type_archive), if yes then include the header-shop.php else show the below default code of the file itself.
An Example Snippet:
if ( is_post_type_archive('product')) {
// Load header-shop.php
}else{
// continue with default code
}
Even you can load header-shop.php with the PHP function include_once()
if ( is_post_type_archive('product')) {
include_once 'header-shop.php';
}

Use a custom named php file in a Wordpress theme

I'm building a wordpress theme for someone, based on an older modified theme, and it was using some AJAX scripting with Flash based "holders" for ad areas. I'm trying to use custom name phps to add image widgets to them.
So basically, I am trying to use a <?php get_ad('variantnamehere'); ?> and to get ad-customname.php from my theme files.
I assume I have to register those files within functions.php, but I have no idea how to do that.
Is there any way to do that?
In your theme's functions.php add the following:
require_once( dirname(__FILE__) . '/ad-customname.php' );
Thanks to Artur's feedback, I was able to find what I was looking for, thanks to the article here.
In functions.php, include your files using
include ( dirname(__FILE__) . '/customfilename.php' );
In your theme file where you wish to insert the code within that php file, add:
<?php get_template_part('customfilename');?>

Where is the best spot to place extra files on a Wordpress install?

I am making changes to an existing website that is based on WordPress (some javascript add-ins and a few PHP scripts for Ajax purposes).
Is their any proper directory that I should place all these files? I started off putting them in a folder in the root directory called assets, but then decided maybe they should go with the rest of the Wordpress template and javascripts files? Or should I keep them out of the wp- directories, and simply keep them in the assets folder?
I know its a trivial question, but I like doing things right- having them in directories that make sense.
You can keep all your javascript in a folder in your theme's directory, that is wp-content/themename/.
Concerning Ajax, it's implementation is different in WordPress. You must add a data variable called action in your ajax request and then hook it to a function in your functions.php file. Your ajax url should be wp-admin/admin-ajax.php available through admin_url( 'admin-ajax.php' ) in PHP.
Read AJAX in Plugins for examples.
If it is presentation related I believe you should place it in the corresponding theme folder. If it is a functionality you might want consider wrapping it inside a plugin and placing it in a plugin folder.
If you dont care about abstracting this change and making available to other themes, then I would just simply add it inside the theme folder. You have WP helper functions to get the web path to the current theme folder, or to include js from that folder.
The proper place in WordPress ecosystem is the folder wp-content, as this is the one that you preserve while doing WP upgrades, restorations or migrations.
In that folder, it could be part of a theme (/themes), a plugin (/plugins), an uploaded file (/uploads) or, if the situation requires, a custom folder (/my-custom-content).
/wp-content/themes/your-theme
Here, all presentation related code. It's a common mistake to place general functionality code in the theme's functions.php. The first question to be asked before placing custom functions in this file is:
If I change my theme, will I need this function?
See: Where to put my code: plugin or functions.php?
/wp-content/plugins/your-plugin
Let's say you need to enqueue some CSS or Javascript files, and this should happen whatever theme is being used.
The following sample plugin will load SWFObject (bundled with WP) in the page Map and the front page, as both contain a SWF Flash embed. And, in the rest of the site, it will load the WebFont Loader from Google CDN and a CSS file from within the plugin folder.
<?php
/** Plugin Name: My Enqueue Plugin */
add_action( 'wp_enqueue_scripts', 'enqueue_so_16354990' );
function enqueue_so_16354990() {
global $post;
if( $post->post_name == 'map' || is_front_page() ) {
// Enqueue bundled script
wp_enqueue_script( 'swfobject' );
}
else {
// Enqueue from external location
wp_enqueue_script(
'my-web-font',
'//ajax.googleapis.com/ajax/libs/webfont/1.4.2/webfont.js',
array( 'jquery' ), // Dependencies
time(), // Version, use time to prevent caching
true // Enqueue on footer
);
// Enqueue from within plugin directory
wp_enqueue_style( 'my-css', plugins_url( 'css/my-plugin.css', __FILE__) );
}
}
/wp-content/uploads
Here, your theme or plugin should place all user uploaded content, so it can be managed through WP Media Library screen. And the content should survive any theme swap or plugin de-activation.
/wp-content/custom-folder
Many image gallery plugins use this approach to store their custom media library.
Another use is for Mobile themes, where custom user themes are placed in this folder, so not to lose it on plugin update (as everything in the plugin or theme folders is replaced on upgrades).

Categories