WordPress function.php

WordPress Functions.php file loading bootstrap, jQuery and a local Library

function my_js_scripts() {
/**
*	Use latest jQuery and bootstrap Libs 
*/
if( !is_admin() ){
	wp_deregister_script('jQuery');
	wp_deregister_script('bootstray');
	wp_deregister_script('localLib');
	
	wp_register_script('jQuery', "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"); 
	wp_enqueue_script('jQuery');
	wp_register_script('bootstrab', "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"); 
	wp_enqueue_script('bootstrab');
		/* local Lib depends heavily on Jquery - 3rd. param */
	wp_enqueue_script('localLib',  get_stylesheet_directory_uri().'/scripts/myJSLib.js', array('jquery'),'0.9', false );
	wp_enqueue_script('localLib');
	}
}
  
function my_css_style() {
 /* wp_enqueue_style( 'style-name', get_stylesheet_uri() );  //This is for getting style.css of current theme	*/
   wp_register_style( 'cssStyles', get_template_directory_uri() .'css/genericCSS.css');
   wp_enqueue_style( 'cssStyles' );
   wp_register_style( 'bootstrapStyles', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
   wp_enqueue_style( 'bootstrapStyles' );
} 	

add_action( 'wp_enqueue_scripts', 'my_js_scripts' );
add_action( 'wp_enqueue_scripts', 'my_css_style' );

Run a 2.nd WordPress BLOG within your local XAMPP installation

Create a new MySql Database

Invoke phpmyadmin and create a new Database : 
http://localhost/phpmyadmin
Database Name wppv:      Collation: utf8mb4_unicode_ci

Extract and install WordPress

Extract wordpress to a new Location - like  :  D:\xampp\htdocs\pv

Run : http://localhost/pv/wp-admin/install.php
Datbase Name    : wppw
UserName        : root
Password        :
Database Host   : localhost
Table Prefix    : wp_


Install WordPress 

Configure Workpress
Site Titel : PV Test 
UserName   : admin
Password   : Felixxxxxx
Email      : helmut.hutzler@gmail.com

Test your WordPress Connectivity

  • Default URL:  http://localhost/pv
  • Admin URL:  http://localhost/pv/wp-admin/

Verify your WP settings

D:\xampp\htdocs\pv>more wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wppx');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8mb4');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

Reference

PlugINs and Editor Setting for WordPress Projects

Currently I’m using 2 PlugINs and I’ve disabled WYSIWYG Editor for my WordPress Projects

  • Insert_PHP [ used to run PHP code ]
  • Raw HTML [ need to be version 1.5 – 1.4 converts HTML during saving the Code
  • Disable Visual Editing first User -> Profil -> Disable WYSIWYG Editor

Note :  You CAN’T run [insert_php]…[/insert_php] Block inside a .. Block
Raw Html need to be at version 1.5 !


Insert_PHP Plugin: 
Descriptiuon: 
   Run PHP code inserted into WordPress posts and pages.
  
- In the sample below we use Insert_PHP Plugin to call get_stylesheet_directory_uri() 
- get_stylesheet_directory is used by Child Themes [ here we reference an Image location]

Sample:
<td class="firstImageAbu" rowspan="5"><img src=[insert_php]echo get_stylesheet_directory_uri()."/images/htmlImages/abu1.jpg 
   alt='Sponsoren FCW' width=215 />";[/insert_php]</td>  
    

Raw HTML PlugIN
Descripton:
    Lets you enter any HTML/JS/CSS in your posts without WP changing it, as well as disable 
    automatic formatting on a per-post basis. Usage: Wrap your code in ... tags. 
    To avoid problems, only edit posts that contain raw code in HTML mode. Upgrade to Pro 
    to be able to use Visual editor on the same posts without it messing up the code.

Note: Used that PlugIN for a ControlPanel. Without that code the Buttons get strange aligned when 
using <CR> to beautify your Code !

Sample:

<div id="AbuControlPanel">
  <input id="infoButton" class="mediumButton" type="submit" value="" />
  <input id="homeButton" class="mediumButton" type="submit" value="" />
  <div id="AbuButtonGroup" class="button-group">
    <input id="stopButton" class="mediumButton" type="submit" value="" />
    <input id="playButton" class="mediumButton" type="submit" value="" />
    <input id="prevButton" class="mediumButton" type="submit" value="" />
    <input id="nextButton" class="mediumButton" type="submit" value="" />
  </div>
</div>

WordPress: Using Duplicator PlugIN

Step 1:  Install WP Plugin: Duplicator 1.1.8

  • Nothing to add here just install the WordPress Plugin

 

Step2 : Create a Package and upload Installer.php and archive to your local host

  • Duplicator -> Package -> Create New -> Next

Create a New Package

Duplicator1

  • You can ignore the Database Warning [ see video below ]

Lets have a closer look on the PHP errors

Duplicator2

  • Ignoring that Warning for now – but notice that I’m not able to restore that package on the original Server again —>  Not a good  situation but lets move forward 
  • Check:  

 

BUILD the package

  • Finally if we don’t get a timeout you will see the following message .

Duplicator3

  • Click on both buttons to download the both files
  •  Note : Even you may get a Timeout the action may still run successfully to the end

 

Step 3 Create a MySql Database via phpAdmin

Duplicator4

 

Step 4:  Deploy the WordPress Backup to our local XAMPP webserver

  • Verify both files are under the proper newly created directory [ xampp\htdocs ]

Duplicator5

 

Invoke installer.php and test your MySQL connection

Duplicator6

  • Testing MySql Database worked fine

 

Update Files and Database

Duplicator7

 

Apply Final Steps

  • Test Site
  • Security Cleanup

Duplicator8

 

Testing the duplicated Website

Duplicator9

  • As you see the Website Duplication to our localhost works!
  • Now you can remove the Duplicator Files by running Security Cleanup

Reference

 

Handling Ajax Requests within WORDPRESS

Provide access to global URL admin-ajax.php and export this function to our WP POST

  • Use wp_enqueue_script to make JavaScript file my-ajax2.js accessible in our WP Post
  • Use wp_localize_script() to make URL admin-ajax.php useable in our scripts
  • Even though the admin-ajax.php resides in the admin area of your site, you can use it to
    process interactions from the front-end, as well as the back-end
  • In WordPress all Ajax requests are sent to admin-ajax.php for processing.
  • Modified functions.php :
 
  function my_js_scripts() 
      {
        /* AJAX  request with wp_localize_script */
        wp_enqueue_script( 'hhu-script', get_stylesheet_directory_uri().'/scripts/my-ajax2.js', array( 'jquery' ),'0.9', false );
        wp_localize_script('hhu-script', 'hhu_vars', array('ajaxurl'   => admin_url( 'admin-ajax.php' ))    
      }
   add_action( 'wp_enqueue_scripts', 'my_js_scripts' );

 

Define the AJAX Callback Function

  • To fire on the front-end for both visitors and logged-in users use wp_ajax_nopriv_ and wp_ajax_ action
  • Define a Callback Function named myAjax2() [ Called from our PHP code ]
  • Modified functions.php :
 
  function myAjax2()
     { 
       //get data from the ajax() call -  ID greeting3 is used 
     $greeting = $_POST['greeting3']; 
     $res1 = "<p>".$greeting;
     $res2 ='<br />  - get_template_directory()    : '.get_template_directory();
     $res3 ='<br />  - get_template_directory_uri(): '.get_template_directory_uri();
     $res4 ='<br />  - get_stylesheet_directory(): '.get_stylesheet_directory();
     $res5 ='<br />  - get_stylesheet_directory_uri(): '.get_stylesheet_directory_uri();

     $results = $res1.$res2.$res3.$res4.$res5.'</p>';  //Return String 
     die($results); 
     } 

        // create new Ajax call for WordPress for users logged in and Not logged in  
 add_action( 'wp_ajax_nopriv_myAjax2', 'myAjax2' ); 
 add_action( 'wp_ajax_myAjax2', 'myAjax2' );

The HTML Code

  • ajax3-button button triggers the AJAX request
  • Hidden Input field greeting3 message will be send via AJAX request and its value is read by through $_POST[‘greeting3’] in function myAjax2()
  • HTML ID divp4 is used to display the results of our AJAX request
  
 HTML Code:
   <div class="p2" id="divp2"><b>OPS PANEL: </b> 
      <button id="ajax3-button" ">WP AJAX3</button>
   </div>
   <input type="hidden" name="greeting3" id="greeting3" value="WP Details via AJAX request 3:" /> 
   <div class="p4" id="divp4"><p> Info PANEL - P4 - don't forget to scroll down for last message</p> </div>

 

JavaScript Code: my-ajax2.js triggering the AJAX reqest via JQuery

  • Defines the JavaScript file to be run via action parameter : myAjax2
  • Assigns to JS script parameter greeting the value from HTML ID : greeting3
  • The JavaScript object hhu_vars points to admin-ajax.php [ defined via wp_localize_script in Step 1]
  • Updates the HTML div divp4 with the AJAX call results
  • The action parameter is responsible for making the connection between the JavaScript file and the PHP code.
  • JavaScript Code: my-ajax2.js:
   jQuery(document).ready(function() 
    { 
      var greeting = jQuery("#greeting3").val();    
      jQuery("#ajax3-button").click(function()
        {
          console.log('ajax3-button pressed') ;          
          jQuery.ajax({
           type: 'POST', 
           url :  hhu_vars.ajaxurl,
           data: { action: 'myAjax2', greeting3: greeting  },
           success: function(data, textStatus, XMLHttpRequest)
                      {
                      console.log("Returing form AJAX request - Adding greetings to Div:  #divp4 ");  
                        jQuery("#divp4").html(''); 
                        jQuery("#divp4").append(data); 
                        },
            error: function(XMLHttpRequest, textStatus, errorThrown)
                    { alert(errorThrown); }
                 });  
      console.log('Leaving ajax3-button Ajax action');
          });  
  });

Reference