Friday, February 22, 2019

Proper way to use WordPress function with AJAX PHP file

In general, trying to access directly any php file in your theme (or plugin) is going to fail for various reasons.
Using admin-ajax.php is the way to go here. To use it, you would do something like the following:

<?php
$action_name = 'plugin_my_personal_taxonomy';
add_action ('wp_ajax_' . $action_name, array(self::class, 'ajax_call_your_function'));
add_action ('wp_ajax_nopriv_' . $action_name, array(self::class, 'ajax_call_your_function'));

public static function ajax_call_your_function() {
    wp_send_json_success("Hello");
}
And from jQuery:
jQuery(document).ready(function(){
    var $ = jQuery, body = $(document.body);

    $.ajax ({
        url: MY_TAXONOMY_PLUGIN_CORE.ADMIN_URL + '/admin-ajax.php',
        type: 'POST',
        dataType: 'JSON',
        data: {
            action: 'plugin_my_personal_taxonomy'
        },
        success: function (resp) {
            console.log(resp);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            // this error case means that the ajax call, itself, failed, e.g., a syntax error
            // in your_function()
            alert ('Request failed: ' + thrownError.message) ;
        },
    });
});

WordPress | Dynamically reference WordPress site URL in JavaScript | WordPress path url in js script file

During my WordPress development, I often find myself in need of the WP home or blog URL from within the JavaScript code.
Instead of hardcoding url in the JavaScript, we're going to dynamically retrieve the site URL, with a little help in PHP.
<?php
wp_register_script(
    'MY_TAXONOMY_PLUGIN_CORE',
    MY_TAXONOMY_PLUGIN_URL . 'assets/script7.js',
    array('jquery')
);

$params = array(
    'BASE_URL' => MY_TAXONOMY_PLUGIN_URL,
    'ADMIN_URL' => admin_url()
);
wp_localize_script( 'MY_TAXONOMY_PLUGIN_CORE', 'MY_TAXONOMY_PLUGIN_CORE', $params);

wp_enqueue_script('MY_TAXONOMY_PLUGIN_CORE');
And from jQuery
alert(MY_TAXONOMY_PLUGIN_CORE.BASE_URL);

Thursday, February 21, 2019

WordPress Plugin: How To Create A Custom Taxonomy In WordPress like Category | Manually Creating Custom Taxonomies

WordPress provides a new method of grouping content by allowing you to create your own custom taxonomies. The core developers have created the register_taxonomy() function to handle the heavy lifting for us. All you have to do is understand how to configure all of the settings to suit your needs.

Below function will initialize custom taxonomy:


<?php
add_action('init', array(self::class, 'create_topics_hierarchical_taxonomy'), 0);

public static function create_topics_hierarchical_taxonomy() {
    $labels = array(
        'name' => _x( 'Color', 'taxonomy general name' ),
        'singular_name' => _x( 'Colors', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Colors' ),
        'all_items' => __( 'All Colors' ),
        'parent_item' => __( 'Parent Color' ),
        'parent_item_colon' => __( 'Parent Color:' ),
        'edit_item' => __( 'Edit Color' ),
        'update_item' => __( 'Update Color' ),
        'add_new_item' => __( 'Add New Color' ),
        'new_item_name' => __( 'New Color Name' ),
        'menu_name' => __( 'Colors' ),
    );
    register_taxonomy(MY_TAXONOMY_PLUGIN_CORE . 'color', array('post', 'page'), array(
        //If you want parent color option, then enable below parameter to true
        'hierarchical' => false,
        'labels' => $labels,
        //will show settings create/edit/delete options in post/page menu
        'show_ui' => true,
        //will show count posts
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'taxonomy-color')
    ));

    //Default taxonomy will be created on initialization
    wp_insert_term("Black", MY_TAXONOMY_PLUGIN_CORE . "color");
    wp_insert_term("Blue", MY_TAXONOMY_PLUGIN_CORE . "color");
    wp_insert_term("Yellow", MY_TAXONOMY_PLUGIN_CORE . "color");
    wp_insert_term("Red", MY_TAXONOMY_PLUGIN_CORE . "color");
}

Below function will add required script in taxonomy page:


<?php
add_action('admin_enqueue_scripts', array(self::class, 'my_add_admin_scripts'));

public static function my_add_admin_scripts() {
    global $pagenow;

    $taxonomy = MY_TAXONOMY_PLUGIN_CORE . 'color';
    if($pagenow == 'edit-tags.php' && (isset($_GET['taxonomy']) && $_GET['taxonomy'] == $taxonomy) && !isset($_GET['action']))
    {
        wp_register_script(
            'quick-edit-js',
            MY_TAXONOMY_PLUGIN_URL . 'assets/script7.js',
            array('jquery')
        );
        wp_enqueue_script('quick-edit-js');
    }
}

Below function will add additional field to our custom taxonomy:


<?php
$taxonomy = MY_TAXONOMY_PLUGIN_CORE . 'color';
$taxonomy1 = 'manage_edit-' . $taxonomy . '_columns';
add_filter($taxonomy1, array(self::class, 'register_category_columns'));

public static function register_category_columns($columns) {
    $columns['my-taxonomy-plugin-color-f1'] = __( 'Creator', 'my-taxonomy-plugin' );
    return $columns;
}

Below function will show custom taxonomy field in taxonomy table


<?php
$taxonomy = MY_TAXONOMY_PLUGIN_CORE . 'color';
$taxonomy2 = 'manage_' . $taxonomy . '_custom_column';
add_filter($taxonomy2, array(self::class, 'category_column_display'), 10, 3);

public static function category_column_display($string, $column_name, $term_id) {
    return esc_html(get_term_meta($term_id, $column_name, true));
}

Below function will allow our custom field of taxonomy to edit in quick edit mode


<?php
$taxonomy = MY_TAXONOMY_PLUGIN_CORE . 'color';
add_action('quick_edit_custom_box', array(self::class, 'quick_edit_taxonomy_field'), 10, 2);
$taxonomy3 = 'edited_' . $taxonomy;
add_action($taxonomy3, array(self::class, 'quick_edit_save_category_field'));

public function quick_edit_taxonomy_field($column_name, $screen) {
    if ( $screen != 'edition' && $column_name != 'my-taxonomy-plugin-color-f1' ) {
        return false;
    }
    include_once MY_TAXONOMY_PLUGIN_DIR . "templates/taxonomy_quick_edit.php";
}

public function quick_edit_save_category_field($term_id) {
    if (isset($_POST['my-taxonomy-plugin-color-f1'])) {
        update_term_meta(
            $term_id, 'my-taxonomy-plugin-color-f1',
            $_POST['my-taxonomy-plugin-color-f1']
        );
    }
}
taxonomy_quick_edit.php
<fieldset>
    <div class="inline-edit-col inline-edit-col-taxonomy-my-color">
        <label>
            <span class="title"><?php _e( 'Creator', 'my-taxonomy-plugin' ); ?></span>
            <span class="input-text-wrap"><input type="text" name="<?php echo esc_attr($column_name); ?>" class="ptitle" value=""></span>
        </label>
    </div>
</fieldset>
script7.js
jQuery(document).ready(function(){
    var $ = jQuery, body = $(document.body);
    (function ($, inlineEditTax) {
        // inlineEditTax does not invoke any events, but does ensure to stop
        // propagation to all other event handlers; swap it out.
        inlineEditTax.editPreMyPlugin = inlineEditTax.edit;
        inlineEditTax.edit = function (link) {
            // Invoke original edit event handler.
            this.editPreMyPlugin(link);

            var tr = $(link).closest('tr[id]');

            var tag_id = tr.attr('id');
            var field1_value = tr.find(".column-my-taxonomy-plugin-color-f1").text();
            $(':input[name="my-taxonomy-plugin-color-f1"]', '.inline-edit-row').val(field1_value);

            return false;
        }
    })($, inlineEditTax);
});

Below function will add a custom field to add/edit form of taxonomy and data will be saved accordingly


<?php
add_action($taxonomy . '_add_form_fields', array(
    self::class, 'taxonomy_metadata_add'
), 10, 1);
add_action($taxonomy . '_edit_form', array(
    self::class, 'taxonomy_metadata_edit'
), 10, 1);
add_action('created_' . $taxonomy, array(
    self::class, 'save_taxonomy_metadata'
), 10, 1);
add_action('edited_' . $taxonomy, array(
    self::class, 'save_taxonomy_metadata'
), 10, 1);

public static function taxonomy_metadata_add($tag) {
    if (current_user_can( 'publish_posts')) {
        include_once MY_TAXONOMY_PLUGIN_DIR . "templates/taxonomy_add.php";
    }
}

public static function taxonomy_metadata_edit($tag) {
    if (current_user_can( 'publish_posts')) {
        ob_start();
        global $my_taxonomy_plugin_color_f1;
        $my_taxonomy_plugin_color_f1 = get_term_meta($tag->term_id, 'my-taxonomy-plugin-color-f1', true);
        include_once MY_TAXONOMY_PLUGIN_DIR . "templates/taxonomy_edit.php";
        $content = ob_get_contents();
        ob_end_clean();
        echo $content;
    }
}

public static function save_taxonomy_metadata($term_id) {
    if ( isset($_POST['my-taxonomy-plugin-color-f1']) ) {
        update_term_meta($term_id, 'my-taxonomy-plugin-color-f1', esc_attr($_POST['my-taxonomy-plugin-color-f1']));
    }
}
taxonomy_add.php
<div class="form-field">
    <label for="my-taxonomy-plugin-color-f1"><?php _e('Creator'); ?></label>
    <input name="my-taxonomy-plugin-color-f1" id="my-taxonomy-plugin-color-f1" type="text" value="" size="70" />
    <p class="description"><?php _e('Title display in creator is limited to 70 chars'); ?></p>
</div>
taxonomy_edit.php
<table class="form-table">
    <tbody>
    <tr class="form-field form-required term-name-wrap">
        <th scope="row"><label for="my-taxonomy-plugin-color-f1"><?php _e('Creator'); ?></label></th>
        <td>
            <input name="my-taxonomy-plugin-color-f1" id="my-taxonomy-plugin-color-f1" type="text" value="<?= esc_attr($my_taxonomy_plugin_color_f1) ?>" size="70" />
            <p class="description"><?php _e('Title display in creator is limited to 70 chars'); ?></p>
        </td>
    </tr>
    </tbody>
</table>

Now we will add metabox to append our taxonomy to post edit page


<?php
add_action('admin_init', array(Init::class, 'check_if_plugin'));

public static function check_if_plugin() {
    if (is_plugin_active(MY_TAXONOMY_PLUGIN_NAME)) {
        add_meta_box(
            MY_TAXONOMY_PLUGIN_CORE . 'metabox1', __( 'Choose Colors', MY_TAXONOMY_PLUGIN_CORE ),
            array(Init::class, 'my_meta_box_data_callback'), 'post'
        );
        add_action('save_post', array(self::class, "my_taxonomy_post_save_callback"), 1, 2);
    }
}

public static function my_taxonomy_post_save_callback($id, $post) {
    if (!isset($_POST[MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME])) {
        return $post->ID;
    }
    if (!wp_verify_nonce($_POST[MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME], MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME)) {
        return $post->ID;
    }
    if (!current_user_can( 'edit_post', $post->ID)) {
        return $post->ID;
    }

    $options = isset($_POST[self::getTaxonomyName()]) ? $_POST[self::getTaxonomyName()] : array();
    wp_set_post_terms($post->ID, $options, self::getTaxonomyName());

    return $post->ID;
}

public static function my_meta_box_data_callback() {
    global $post;
    $taxonomy_options = get_terms(array(
        'taxonomy' => self::getTaxonomyName(),
        'hide_empty' => false,
    ));
    if (count($taxonomy_options) > 0) {
        $post_terms = [];
        foreach(wp_get_post_terms($post->ID, self::getTaxonomyName()) as $o) {
            $post_terms[$o->term_id] = 1;
        }

        global $taxonomy_data_map;
        $taxonomy_data_map = [];
        $taxonomy_data_map['class'] = self::getTaxonomyName();
        $taxonomy_data_map['options'] = $taxonomy_options;
        $taxonomy_data_map['selected'] = $post_terms;

        include_once MY_TAXONOMY_PLUGIN_DIR . 'templates/taxonomy_post_edit.php';
        wp_nonce_field( MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME, MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME );
    }
}

taxonomy_post_edit.php
<?php
global $taxonomy_data_map;
$taxonomy_class = $taxonomy_data_map['class'];
$taxonomy_options = $taxonomy_data_map['options'];
$taxonomy_selected = $taxonomy_data_map['selected'];
?>

<div class="<?= $taxonomy_class ?>-editor-on-post">
    <h2>
        Colors
    </h2>
    <?php foreach ($taxonomy_options as $o) { ?>
        <div class="<?= $taxonomy_class ?>-taxonomies__hierarchical-terms-list" tabindex="0" role="group" aria-label="Available Colors">
            <?php $_checked = isset($taxonomy_selected[$o->term_id]) ? 'checked' : ''; ?>
            <div class="<?= $taxonomy_class ?>-taxonomies__hierarchical-terms-choice">
                <input
                        id="<?= $taxonomy_class ?>-taxonomies-hierarchical-term-<?= $o->term_id ?>"
                        class="<?= $taxonomy_class ?>-taxonomies__hierarchical-terms-input"
                        type="checkbox" value="<?= $o->name ?>"
                        name="<?= $taxonomy_class ?>[]" <?= $_checked ?>
                />
                <label for="<?= $taxonomy_class ?>-taxonomies-hierarchical-term-<?= $o->term_id ?>"><?= esc_attr($o->name) ?></label>
            </div>
        </div>
    <?php } ?>
    <div style="clear:both"></div>
</div>

Now time to display our taxonomy data to post details view


<?php
add_filter('the_content', array(self::class, 'my_the_content'));

public static function my_the_content($c) {
    if (!is_admin() && !is_feed() && (is_single() || is_page())) {
        global $post;
        $allow = !is_null($post) && property_exists($post, "post_type") && in_array($post->post_type, array('post'));
        if ($allow) {
            $content = array();
            foreach(wp_get_post_terms($post->ID, self::getTaxonomyName()) as $o) {
                $content[] = $o->name;
            }
            $c = $c . "<div class=\"my_taxonomy_plugin_color-post-display\">
                            <div class=\"row\">
                            <label class=\"label\">Colors</label>
                            <span class=\"value\">" . implode(', ', $content) . "</span>
                        </div>
                    </div>";
        }
    }
    return $c;
}

Below are some of screen shots





d

Download full plugin from BitBucket


Monday, February 18, 2019

WordPress | Disable posts auto saving

To disable WordPress autosaving function, simply open your functions.php file and paste the following function
<?php
function disable_auto_saving_post(){
    wp_deregister_script('autosave');
}
add_action('wp_print_scripts', 'disable_auto_saving_post');

WordPress | Automatically Insert Content After Each WordPress Post or Page

For example, on all the our posts and pages, we have an email subscription box. There are several ways to add content to your post, without having to do so manually
Most efficient way is using a filter to add content to your post content
<?php
define("MY_FIRST_PLUGIN_DIR", plugin_dir_path(__FILE__));
add_filter('the_content', 'my_the_content');
function my_the_content($c) {
    if (!is_admin() && !is_feed() && (is_single() || is_page())) {
        global $post;
        $allow = !is_null($post) && property_exists($post, "post_type") && in_array($post->post_type, array('post', 'page'));
        if ($allow) {
            ob_start();
            include MY_FIRST_PLUGIN_DIR . "templates/page_content.php";
            $content = ob_get_contents();
            ob_end_clean();
            $c = $c . $content;
        }
    }
    return $c;
}

Sunday, February 17, 2019

Wordpress create table on Plugin activation if not exists

<?php
global $wpdb;
$table1 = $wpdb->prefix . "my_table";

if($wpdb->get_var( "show tables like '$table1'" ) != $table1) {
    $sql = "CREATE TABLE `". $table1 . "` ( ";
    $sql .= "  `post_id` BIGINT NULL, ";
    $sql .= "  `field1` VARCHAR(250) NULL, ";
    $sql .= "  `field2` VARCHAR(250) NULL, ";
    $sql .= "  INDEX `index_by_post_id` (`post_id`) ";
    $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 ; ";
    require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
    dbDelta($sql);
}

Friday, February 8, 2019

GRAILS > FUNCTIONALITY AFTER TRANSACTION COMMITTED

So the problem is when we working on grails project with hibernate transaction to manage data, we do not know when actually transaction committed or not. But sometimes we have to wait until current transaction finally committed. Because unless transaction committed, this data will not available for other transactions. Below is a sample project showing how to notified or when actually current transaction committed. Its some wired way but its working.

THIS PROJECT FOR DEMONSTRATE HOW TO DO SOME ADDITIONAL WORK AFTER AN TRANSACTION COMMITED

1. First setup DataSource.groovy
2. Add *mavenRepo "https://oauth.googlecode.com/svn/code/maven"* in *repositories* to BuildConfig.groovy
3. Add below dependencies to *dependencies* in BuildConfig.groovy
    compile "org.springframework:spring-orm:$springVersion"
    runtime 'mysql:mysql-connector-java:5.1.29'
    runtime 'org.springframework:spring-test:4.0.5.RELEASE'
4. Add a controller named *HomeController.groovy*
5. Add a service named *HomeService.groovy*
6. Add a domain named *Home.groovy*

MOST IMPORTANT PARTS:
7. Add below groovy files
named *org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTransactionManager.groovy*
named *org.codehaus.groovy.grails.orm.hibernate.TransactionStatus.groovy*
named *org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.groovy*
under *src/groovy*

8. You are done! See *HomeService.groovy* to see how you can use this feature in Grails project.

package org.codehaus.groovy.grails.orm.hibernate

import groovy.transform.CompileDynamic
import org.hibernate.FlushMode
import org.springframework.orm.hibernate4.HibernateTransactionManager
import org.springframework.transaction.TransactionDefinition
import org.springframework.transaction.support.DefaultTransactionStatus

class GrailsHibernateTransactionManager extends HibernateTransactionManager {
    @Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {
        super.doBegin transaction, definition
        if (definition.isReadOnly()) {
            setFlushModeManual(transaction)
        }
    }

    @CompileDynamic
    protected void setFlushModeManual(transaction) {
        transaction.sessionHolder?.session?.flushMode = FlushMode.MANUAL
    }

    @Override
    protected DefaultTransactionStatus newTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources) {
        return new TransactionStatus(super.newTransactionStatus(definition, transaction, newTransaction, newSynchronization, debug, suspendedResources))
    }

    @Override
    protected void doCommit(DefaultTransactionStatus status) {
        super.doCommit(status)
        status.triggerCommitHandlers()
    }
}
BELOW IS SAMPLE OUTPUT

package com.pritom

import grails.transaction.Transactional
import org.codehaus.groovy.grails.orm.hibernate.TransactionStatus

@Transactional
class HomeService {
    void saveEntity() {
        TransactionStatus.current.onCommit {
            println("YES!!! TRANSACTION COMMITTED SUCCESSFULLY!!!")
            println("HOME COUNT ${Home.count()}")
        }
        Home home = new Home()
        home.name = "House #${Home.count() + 1}"
        home.addreess = "Lane ${Home.count() + 1}, Dhaka"
        home.save()
    }
}

2019-02-08 11:33:55,048 [http-bio-8080-exec-1] DEBUG hibernate.SQL  - select count(*) as y0_ from home this_
Hibernate: select count(*) as y0_ from home this_
2019-02-08 11:33:55,050 [http-bio-8080-exec-1] DEBUG hibernate.SQL  - select count(*) as y0_ from home this_
Hibernate: select count(*) as y0_ from home this_
2019-02-08 11:33:55,053 [http-bio-8080-exec-1] DEBUG hibernate.SQL  - insert into home (version, addreess, name) values (?, ?, ?)
Hibernate: insert into home (version, addreess, name) values (?, ?, ?)
2019-02-08 11:33:55,054 [http-bio-8080-exec-1] TRACE sql.BasicBinder  - binding parameter [1] as [BIGINT] - [0]
2019-02-08 11:33:55,054 [http-bio-8080-exec-1] TRACE sql.BasicBinder  - binding parameter [2] as [VARCHAR] - [Lane 5, Dhaka]
2019-02-08 11:33:55,054 [http-bio-8080-exec-1] TRACE sql.BasicBinder  - binding parameter [3] as [VARCHAR] - [House #5]
YES!!! TRANSACTION COMMITTED SUCCESSFULLY!!!
2019-02-08 11:33:55,130 [http-bio-8080-exec-1] DEBUG hibernate.SQL  - select count(*) as y0_ from home this_
Hibernate: select count(*) as y0_ from home this_
HOME COUNT 5
You can use multiple TransactionStatus.current.onCommit, each will be called one after one.
You can download sample project from here
GitHub project link
Required JAR file to get this working (if dependecny failed)