Pagename: php - Php
Category:Array
Post Count: 5Category:General
Pagename: php - Php

WordPress – Links (Reference…)

Useful links:

Creating custom tables in wordpress
Clear example of adding ‘custom table’ to wordpress database
Guide to flexboxes
Easy to follow example of flexbox CSS

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:General
Pagename: php - Php

WordPress – Google Analytics (ga) tracking code as a plugin

For wordpress sites I build, I insert the google analytics (ga) tracking code into the html head section via a plugin. The code for this is shown below. Alternatively, it is relatively easy to slip the tracking code directly into the ‘header(s)’ template (Located: your-site.com\wp-content\themes\active-theme\header.php) as described by google analytics on the ‘tracking-code’ page. However, I prefer the ‘cleaner’ approach of separating the code into a separate file. So much easier to read if it is ever required…

I also use the WordPress function ‘is_user_logged_in’ to remove most of my visits to the site as I am typically signed in to my WP admin account.

Here is the code:

<?php
	/*
	Plugin Name: Site-Name Google Analytics Plugin
	Plugin URI: http://--relevant url?
	Description: Adds the Google analytics tracking code to ... Site-Name
	Author: Author name
	Version: 1.0
	 */
	/* 
	Do not insert tracking code if the user is logged in (ie internal traffic) 
	This is wrapped in a function (insert_ga_tracking() ) so it can 
	be loaded with the 'init' hook. An error can be produced otherwise...
	*/
	

	function insert_ga_tracking()
	{
		if ( ! is_user_logged_in() )
		{
			add_action( 'wp_head', 'Site-Name_google_analytics', 10 ); 
			function Site-Name_google_analytics() 
			{ 
?>
				<!-- Global site tag (gtag.js) - Google Analytics -->
				<script async src="https://www.googletagmanager.com/gtag/js?id=UA-12345678-1">
				</script>
				<script>
				  window.dataLayer = window.dataLayer || [];
				  function gtag(){dataLayer.push(arguments);}
				  gtag('js', new Date());

				  gtag('config', 'UA-12345678-1');
				</script>

<?php
			}
		}
	}
	add_action('init', 'insert_ga_tracking');

To use this code…

Create a php file in the folder location: Site-Name/wp-content/plugins/ and add the script above to this file.

The tracking code between ‘‘ and ‘‘ needs the tracking code supplied by google for your site (located in the admin/tracking for your google analytics property).
‘Site-Name’ in the code above can be changed to the target site or removed (in all four locations…). I add the site name as a check to ensure the right tracking code goes to the right site location.

Then, in the WP Admin dashboard, go to ‘installed plugins’ and activate the plugin which should be displayed with the name you specified ‘Plugin name’.

That should be all there is to it but don’t forget to test it using google analytic’s testing feature.

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:General
Pagename: php - Php

WordPress – Child Theme Steps

Summarizing the steps explained in the WP Codex for creating a child theme.

Source:https://developer.wordpress.org/themes/advanced-topics/child-themes/

  1. Select theme to use via WP dashboard -> Appearance -> Themes
  2. Create new folder named after selected theme with ‘-child’ appended to the end (ie ‘Theme Name-child’)
  3. In the theme child folder, add these files:
    1. style.css
    2. functions.php
  4. Insert this header information in the ‘style.css’ file
    /*
     Theme Name:   Twenty Fifteen Child
     Theme URI:    http://example.com/twenty-fifteen-child/
     Description:  Twenty Fifteen Child Theme
     Author:       John Doe
     Author URI:   http://example.com
     Template:     twentyfifteen
     Version:      1.0.0
     License:      GNU General Public License v2 or later
     License URI:  http://www.gnu.org/licenses/gpl-2.0.html
     Tags:         light, dark, two-columns, right-sidebar...
     Text Domain:  twenty-fifteen-child
    */
    
    
  5. Update this information ensuring that:
    1. Theme name is unique (best practice is to use the name of the parent theme with ‘child’ added to the end
    2. Template lists the name of the parent theme
  6. Insert this script into the function.php file (that was created for the child theme)
    <?php 
    
      add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); 
    
      function my_theme_enqueue_styles() { 
    
        // Example: 'parent-style' is 'twentyfifteen-style' 
        //           for the Twenty Fifteen theme
        $parent_style = 'parent-style'; 
    
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); 
    
        wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . 
            '/style.css', array( $parent_style ), wp_get_theme() );
    
        get('Version'));
    
      }
    ?>
    
    
  7. NOTE: Ensure the $parent_style variable is set to the handle used by the parent theme. The handle can be found by searching in the parent’s theme folder for the ‘functions.php’ file and searching for: ‘wp_enqueue_style’. The first argument is the handle to use.
  8. To display an image in the WP theme selection an image named ‘screenshot’ needs to be added to the child theme folder.
  9. Child theme should now be able to be selected in the WP dashboard (Appearance->themes).
  10. Update css in the style sheet, functions in the functions file and copy over any theme templates that you want to modify. Modifications will not impact the parent theme and updates to the parent theme will not erase the changes made in the child theme.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:General
Pagename: php - Php

WordPress – Steps to import MySQL DB from online host to localhost

Steps I use to import data (ie posts, settings info etc) from my online hosting account to the MySQL database on my localhost (ie my own pc). This will not import any new plugins, updates or ploaded images or files. This post covers exporting and importing the wordpress Database.

NOTE: I do not claim this is the most efficient or the most secure method. It is simply how I do it.

I use:

  • XAMPP to run the apache server
  • MySQL workbench to import the DB file onto my pc
  • phpMyAdmin via my online hosts cpanel to create the export file

Steps in short:

  1. Backup database from localhost using MySQL client ( I use MySQL workbench)
  2. Export data from online hosting site using phpMyAdmin
  3. Modify site settings in exported file
  4. Import data from SQL file created in phpMyAdmin

Details:

1. Backup database from localhost using MySQL client. Screen shots shown here are using MySQL workbench…

Export as a single file:

Archive the file for backup. I typically give the file a more meaningful name and I will select the ‘include create schema’ box as shown below:

2. Export data from online hosting site using phpMyAdmin
i. Access hosting account using cpanel
ii. Locate and open phpMyAdmin (in cpanel Database category)
iii. Use phpMyAdmin to export a copy of the desired online database by:

a) Locate database in database list

 

 

 

 

 

 

 

 

b) Use ‘Export’ function to view export options:

 

c) Leave most options but select ‘Add drop table’ to renew all tables within database. Note: select ‘create database’ as well if you intend on refreshing the database completely (you will need to drop the database).

d) Select ‘go’ to run the export function and save the outputed file in a suitable location (ie archives)

 

3) In exported SQL file, look for the site domain name that is specified in ‘wp_options’ table script. For example, looking for site dradma.com…

 

Modify these lines to work on the localhost. For example….

4. Import data from SQL file created in phpMyAdmin using selected MySQL client program. Below shows MySQL workbench process…open SQL file (phpMyAdmin export), run SQL script and check the database has successfully been imported.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:General
Pagename: php - Php

WordPress – Adding programming code to a post

Documentation covers this in detail: https://en.support.wordpress.com/code/posting-source-code/

Notes:

  1. Use ‘Text’ tab in post editor to avoid unnecessary formatting tags being added
  2. Structure (using ‘vb’ as an example):

    [code language="vb"] ...your code here... [/code]

Example (using VB):

[code language=”vb”]
Sub ShowAllWorkSheets()
‘Makes all worksheets visible
Dim CWS As Worksheet

For Each CWS In ThisWorkbook.Worksheets
CWS.Visible = xlSheetVisible
Next CWS
End Sub
[/code]

Produces:

Sub ShowAllWorkSheets()
    'Makes all worksheets visible
    Dim CWS As Worksheet
     
    For Each CWS In ThisWorkbook.Worksheets
        CWS.Visible = xlSheetVisible
    Next CWS
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *