SoftTouch API Integration with WordPress using SOAP(cURL method)

Hi guys,
here is the code snippete to Integrate third-party API(Web Service) in WordPress, This is the post related to Integrate the SoftTouch API, using the SOAP client with cURL method. (using only the API Access Token method)

To initiate, connection and get products from SoftTouch API, following is the code snippete:

$authToken = 'your-api-access-token';  
$base_url = 'your-api-base-url-with-account-id'; //eg. https://api.softtouch.eu/1/accounts/your-account-id-here/
base_url .= 'pass-your-api-requet-here'; //eg. if you want to get first 100 products then use, products?take=100
$httpRequest = curl_init();
curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array('Authorization: ' . $authToken, 'Content-Type: application/json'));
curl_setopt($httpRequest, CURLOPT_URL, $base_url);
curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($httpRequest);
if ($response1 === FALSE){
    die(curl_error($httpRequest));
}
curl_close($httpRequest);
$product_arr = json_decode($response);

thats it, in the $product_arr variable you will get first 100 products array response.

Note: Similarly, you can use different types of API requests according to your requirements.

Thank you,

Advertisements

Skarabee API Integration with WordPress

Hi guys,
here is the code snippete to Integrate third-party API(Web Service) in WordPress, This is the post related to Integrate the Skarabee API.

Include the composed file system in your current active theme, put the below code in your theme’s functions.php file

require_once __DIR__ . '/WebServiceSOAP/examples/credentials.php';
require_once __DIR__ . '/WebServiceSOAP/src/Skarabee.php';

here you will find the .zip file which you need to include in your active theme directory.
Download WebServiceSOAP.zip

Extrat and keep that folder (WebServiceSOAP) in your active theme directory, and set you API credentials in WebServiceSOAP/examples/credentials.php file

then the following:
For all the call-backs, you need to initiate the API connection, using following code snippete

$api = new Skarabee($username, $password);

1. To get All Publications, call the following Function:

$AllPublications = $api->getAll();

2. To get Single Publication, call the following Function:

$SinglePublication = $api->get($publicationID);

3. To get All Projects, call the following Function:

$SinglePublication = $api->getAllProjects();

4. To get Contact Info., call the following Function:

$SinglePublication = $api->getContactInfo();

Thank you,

Set global AJAX JS/URL in WP custom theme

Hi guys,
here is the code snippete to set global AJAX JS/URL in WP custom theme

to include JS files(include JS files to front-end), add the following code to your theme’s functions.php file.

add_action( 'wp_enqueue_scripts', 'mytheme_load_scripts' );
function mytheme_load_scripts(){
    wp_register_script('myajax-js', get_template_directory_uri() . '/js/ajax-script.js',false,true);
    wp_enqueue_script('myajax-js');  
    $translation_array = array( 'templateUrl' => admin_url( 'admin-ajax.php' ) );
    wp_localize_script( 'myajax-js', 'object_name', $translation_array );
}

you can use this global ajax-url in /js/ajax-script.js, to call ajax

var ajaxurl = object_name.templateUrl;
$(document).on('click', '.element', function(){
      $.ajax({
        url : ajaxurl,
        type : 'post',
        dataType:'html',
        data : {
          action : 'call_any_function_here',
        },
        success : function( response ) {         
          if(response.ajaxComplete == 'true'){
            //
          }
        }
      });
    });

Thank you,

Create custom post type in wp theme

Hi guys,
here is the code snippete to create custom post type in wp theme

to create custom post type in wp theme, add the following code to your theme’s functions.php file.

function mytheme_custom_post_portfolio() {
    $labels = array(
        'name' => _x('Portfolio', 'post type general name', 'mytheme'),
        'singular_name' => _x('Portfolio', 'post type singular name', 'mytheme'),
        'add_new' => _x('Add New Portfolio', 'book', 'mytheme'),
        'add_new_item' => __('Add New Portfolio', 'mytheme'),
        'edit_item' => __('Edit Portfolio', 'mytheme'),
        'new_item' => __('New Portfolio', 'mytheme'),
        'view_item' => __('View Portfolio', 'mytheme'),
        'search_items' => __('Search Portfolio', 'mytheme'),
        'not_found' =>  __('No Portfolio found', 'mytheme'),
        'not_found_in_trash' => __('No Portfolio found in Trash', 'mytheme'), 
        'parent_item_colon' => ''
    );      
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'has_archive' => true,
        'supports' => array('title', 'thumbnail'),
        'menu_icon' => 'dashicons-format-gallery'
    );      

    register_post_type( 'portfolio', $args );

    $labels = array(              
      'name' => _x( 'Portfolio Categories', 'taxonomy general name' ),
      'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Portfolio Categories' ),
      'all_items' => __( 'All Portfolio Categories' ),
      'parent_item' => __( 'Parent Portfolio Category' ),
      'parent_item_colon' => __( 'Parent Portfolio Category:' ),
      'edit_item' => __( 'Edit Portfolio Category' ), 
      'update_item' => __( 'Update Portfolio Category' ),
      'add_new_item' => __( 'Add New Portfolio Category' ),
      'new_item_name' => __( 'New Portfolio Category Name' ),
       
    );

    register_taxonomy(
        'portfoliocats',
        'portfolio',
        array(
            'public'=>true,
            'hierarchical' => true,
            'labels'=> $labels,
            'query_var' => 'portfoliocats',
            'show_ui' => true,
            'rewrite' => array( 'slug' => 'portfoliocats', 'with_front' => false ),
        )
    );        
}                                  
add_action('init', 'mytheme_custom_post_portfolio');

Thank you,

Custom function for detecting Mobile/Ipad/Desktop in wordpress theme

Hi guys,
here is the code snippete for detecting Mobile/Ipad/Desktop in wordpress theme

to add for Mobile/Ipad/Desktop detecting function in wordpress theme, add the following code to your theme’s functions.php file.

function my_wp_is_mobile() {
    static $is_mobile;

    if ( isset($is_mobile) )
        return $is_mobile;

    if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
        $is_mobile = false;
    } elseif (
        strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false ) {
            $is_mobile = true;
    } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') == false) {
            $is_mobile = true;
    } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false) {
        $is_mobile = true;
    } else {
        $is_mobile = false;
    }

    return $is_mobile;
}

to detect Mobile/Ipad/Desktop, add the following code to where you want to detect Mobile/Ipad/Desktop.

if(my_wp_is_mobile()){
	//when it is mobile
}else{
	//when it is desktop
}

Thank you,

Create custom Theme Options Panel for wp custom theme using Codestar Framework

Hi guys,
here is the code snippete custom Theme Options Panel for wp custom theme using Codestar Framework

to integrate/include Codestar Framework, add the following code to your theme’s functions.php file.

require_once dirname( __FILE__ ) .'/cs-framework/cs-framework.php';

here you can find the cs-framework folder, add this cs-framework folder in your custom theme.
https://drive.google.com/file/d/0B1BSUwinVFKzLXpGTmIxQ3FEWTA/view?usp=sharing

in the file /cs-framework/config/framework.config.php
there is demo theme options

for reference of check all files in the folder /cs-framework/config/

Thank you,

Create custom Theme Options Panel for wp custom theme using Redux Framework

Hi guys,
here is the code snippete custom Theme Options Panel for wp custom theme using Redux Framework

to integrate/include Redux Framework, add the following code to your theme’s functions.php file.

require_once (dirname(__FILE__) . '/redux/redux-framework.php');
if ( class_exists( 'Redux' ) ) {
	require_once (dirname(__FILE__) . '/redux/sample/barebones-config.php');
}

here you can find the redux folder, add this redux folder in your custom theme.
https://drive.google.com/file/d/0B1BSUwinVFKzR0t1ZHFLUUxmblE/view?usp=sharing

in the file /redux/sample/barebones-config.php
on line 200, there is demo theme options for header and footer

for reference of all in-built fields, you can check this file /redux/sample/sample-config.php

Thank you,

Create custom meta-box for wp posts and pages using CMB2

Hi guys,
here is the code snippete to integrate/include CMB2 library and custom-meta-box fields

to integrate/include CMB2 library and custom-meta-box fields, add the following code to your theme’s functions.php file.

add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
    if ( !class_exists( 'cmb_Meta_Box' ) ) {
        require_once( get_template_directory() ."/customs/CMB2/init.php" );
    }
}
require ( get_template_directory() . "/customs/CMB2/metaboxes.php");

here you can find the customs folder, add this customs folder in your custom theme.
https://drive.google.com/file/d/0B1BSUwinVFKzYTUyX0FoUk0yd3M/view?usp=sharing

in the file /customs/CMB2/metaboxes.php
on line 480, there is custom post’s sample meta-box
on line 500, there is custom page-template’s sample meta-box

for reference of meta-fields, you can check this file /customs/CMB2/example-functions.php

Thank you,

Custom image resize/cropping in wp

Hi guys,
here is the code snippete to custom image resize/cropping in wp

to custom pagination for the custom image resize/cropping in wp, add the following code to your theme’s functions.php file.

function custom_resize($url, $width, $height = null, $crop = null, $single = true) {
    if (!$url OR !$width)
    return false;

    $upload_info = wp_upload_dir();
    $upload_dir = $upload_info['basedir'];
    $upload_url = $upload_info['baseurl'];

    if (strpos($url, $upload_url) === false)
        return false;

    $rel_path = str_replace($upload_url, '', $url);
    $img_path = $upload_dir . $rel_path;

    if (!file_exists($img_path) OR !getimagesize($img_path))
    return false;

    $info = pathinfo($img_path);
    $ext = $info['extension'];
    list($orig_w, $orig_h) = getimagesize($img_path);

    $dims = image_resize_dimensions($orig_w, $orig_h, $width, $height, $crop);
    $dst_w = $dims[4];
    $dst_h = $dims[5];

    $suffix = "{$dst_w}x{$dst_h}";
    $dst_rel_path = str_replace('.' . $ext, '', $rel_path);
    $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";

    if (!$dst_h) {
        $img_url = $url;
        $dst_w = $orig_w;
        $dst_h = $orig_h;
    }
    elseif (file_exists($destfilename) && getimagesize($destfilename)) {
        $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
    }
    else {
        if (function_exists('wp_get_image_editor')) {

            $editor = wp_get_image_editor($img_path);

            if (is_wp_error($editor) || is_wp_error($editor->resize($width, $height, $crop)))
                return false;

            $resized_file = $editor->save();

            if (!is_wp_error($resized_file)) {
                $resized_rel_path = str_replace($upload_dir, '', $resized_file['path']);
                $img_url = $upload_url . $resized_rel_path;
            } else {
                return false;
            }
        } else {

            $resized_img_path = image_resize($img_path, $width, $height, $crop);
            if (!is_wp_error($resized_img_path)) {
                $resized_rel_path = str_replace($upload_dir, '', $resized_img_path);
                $img_url = $upload_url . $resized_rel_path;
            } else {
                return false;
            }
        }
    }

    if ($single) {
        $image = $img_url;
    } else {
        $image = array(
            0 => $img_url,
            1 => $dst_w,
            2 => $dst_h
        );
    }
    return $image;
}

//added theme-support for image cropping
if (function_exists('add_theme_support')) {
    add_theme_support('post-thumbnails');
    add_image_size('large-full', 1920, 781, true); 
    add_image_size('full', 1180, 440, true); 
    add_image_size('large', 950, 480, true);
    add_image_size('medium', 440, 330, true);
    add_image_size('thumbnail', 250, 187, true);
}

use custom image cropping function in image tag

// custom_resize($image_url,600,400,true);
// width = 600, height = 400, true = hard crop
<img src="<?php echo custom_resize( $image_url, 600, 400, true ); ?>" alt="<?php the_title(); ?>" />

Thank you,

Custom pagination for the custom posts loop

Hi guys,
here is the code snippete to custom pagination for the custom posts loop

to custom pagination for the custom posts loop, add the following code to your theme’s functions.php file.

function my_pagination($pages = '', $range = 2){  
	$showitems = ($range * 2)+1;  
	global $paged;
	if(empty($paged)) $paged = 1;

	if($pages == ''){
		global $wp_query;
		$pages = $wp_query->max_num_pages;
		if(!$pages){
			$pages = 1;
		}
	}   

	if(1 != $pages){
		echo '<div class="center"> <ul class="pagination">';
		if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>&laquo;</a></li>";
		if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a></li>";

		for ($i=1; $i <= $pages; $i++){
			if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
				echo ($paged == $i)? "<li class='active'><a>".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>";
			}
		}

		if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."' aria-label='Next' class='next'><span aria-hidden='true'>Next</span></a>";  
		if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'  aria-label='Previous' class='prev'><span aria-hidden='true'>Previous</span></a>";
		echo "</ul></div>\n";
	}
}

use custom pagination in the custom posts loop

global $paged;
if(empty($paged)) $paged = 1;
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged));

if ( $loop ) :
    while ( $loop->have_posts() ) : $loop->the_post();
        //show posts here 
    endwhile;
endif;

if(function_exists('stw_pagination')):
    stw_pagination($loop->max_num_pages);
endif;

Thank you,