Ahhh, wordpress, making things so easy it seems. Definitely true if you want to have a blog up and running in a few minutes but simplicity is dropped like a red hot coal as soon as the user wants to start doing anything outside the box. Intuitive interfaces are quickly replaced with visits to codex.wordpress.org.

Here I by no means cover Wordpress in amount of detail. Wordpress posts cover my own experience in setting up blogs which may or may not help others going through a similar process.Pagename: wordpress - Wordpress
Category:Array
Post Count: 5Category:Wordpress
Pagename: wordpress - Wordpress

WordPress – Moving wordpress installation to subfolder

After installing wordpress into home directory (ie public_html ) I realised it is not as clean as one would want when working with numerous addon domains. Far better, in my opinion, to have the wordpress folders and files in a separate sub-folder for the sake of tidyness.

Sites I used for this information were:

  1. askwpgirl – move-wordpress-from-subdirectory-to-root-directory
  2.  

The steps I took are:

    1. Create sub-folder to use
    2. Move wordpress folders & files to new sub-folder
    3. Logged into the wordpress console using the url with the sub-folder included (ie example.com/sub-folder/wp-admin)
    4. Specify sub-folder in wordpress’s general settings:
    5.  

 

Steps taken to move from home folder to separate sub-folder:

Leave a Reply

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

Category:Wordpress
Pagename: wordpress - Wordpress

WordPress – Merge wp_postmeta table (local version with online)

When extra information is required to be stored for a specific record in ‘wp_posts’, it can be stored in the ‘wp_postmeta’ table. One such bit of information is when an attached image is to be used as the site-icon. I realised this when I added an icon (ie a favicon) to a local version of my site only to not see it not show up when I uploaded the files to the online version. I had updated the wp_post table but this also did not rectify the situation. The wp_postmeta table specifies that the image is to be used as the site icon.

I wasn’t sure how well the online data would match with my local version so I did some checks which I describe here for reference:

1. wp_postmeta was exported to local drive and a temporary copy was made (called wp_postmetab).

2. Next I did some exploration. I did a union of both tables and grouped them. In this case, most records matched with the exception of ‘_edit_lock’ field. This made sense as it would change when a post was opened and updated. In this case, I was able to delete the ‘_edit_lock’ records in 1 table and use those from the other table.
Using the query shown below, I looked for ‘counts’ of 1 which would indicate either unique entries or non-matching entries. I was ignoring ‘meta_id’ as it is not used/recorded elsewhere as far as I have been able to determine (this site details that: wp-staging.com/docs/the-wordpress-database-structure/#wp_posts).
Cases where a unique value existed in either table would just be added to the merged table. If the information is needed, it will be there and if not, no big deal that it is there.

For clarity, the query below produces a union of two tables with extra columns to show when records are grouped and which database/table the record(s) come from.

SELECT Count(*) as cnt, SUM(DB1) as Sum1, SUM(DB2) as Sum2, UN.* FROM
(
   SELECT 1 as DB1, 0 as DB2, pm.* FROM DB_Name.wp_postmeta as pm
   UNION ALL
   SELECT 0 as DB1, 1 as DB2, pmb.* FROM DB_Name.wp_postmetab as pmb
) UN
GROUP BY post_id, meta_key, meta_value;

3. For those cases where post_id and meta_key matched but meta_value did not, a decision had to be made for each record. I created a copy of the combined tables without the ‘meta_id’ column (this column was added after). Using ‘UNION’ rather than ‘UNION ALL’ keeps distinct values only. I compared against the previous temporary table for those records that needed to be deleted or amended.

CREATE TABLE TempPostMeta
SELECT post_id, meta_key, meta_value FROM DB_Name.wp_postmeta as pm
UNION
SELECT post_id, meta_key, meta_value FROM DB_Name.wp_postmetab as pmb;

ALTER TABLE TempPostMeta
ADD meta_id bigint(20) unsigned AUTO_INCREMENT PRIMARY KEY FIRST;

4. Once TempPostMeta table was considered a suitable replacement I truncated the data in the wp_postmeta and replaced with the data from TempPostMeta table.

TRUNCATE TABLE DB_Name.wp_postmeta;

INSERT INTO DB_Name.wp_postmeta(meta_id, post_id, meta_key, meta_value)
SELECT meta_id, post_id, meta_key, meta_value from DB_Name.TempPostMeta;

5. I then did the same steps to add the data to the online version and tested which found everything to be working as it should be.

Leave a Reply

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

Category:Wordpress
Pagename: wordpress - Wordpress

WordPress – Add Custom Content Plugin

Editing posts directly within the wordpress editor can be a nuisance when wordpress inserts ‘p’ tags and the like. Sometimes I want to edit HTML in actual HTML editor which allows me greater control over the presentation of the content.

To facilitate this I have added a plugin which inserts HTML from a custom file based on the post id.

Content is added from a file within the ‘custom’ folder:
“current-theme-child\Custom\Content\Custom-Content-82.php

This file can be edited and viewed in a standard HTML editor. The number within the file name is the post id which can be found from url’s and other ways. When the content is shown, the content within the wordpress editor is shown first followed by the content within the file.

Here is the plugin code as to date:

?php
/**
 * Plugin Name:       Include Custom Content
 * Plugin URI:        https://dalestake.com/2019/11/26/wordpress-add-custom-content-plugin/
 * Description:       Inserts files stored in 'Custom/Content/' into the content section (ie within the body of the post)
 * Version:           0.1
 * Author:            Dale Anderson
 * Author URI:        NA
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       include-custom-content
 */

// Check if a custom content file exists... 


add_action('wp_head','CheckForCustomContent');
$BaseCustomDir = "";
$CurID = "";
$FilePath = "";

function CheckForCustomContent()
{
	global $BaseCustomDir;
	global $CurID;
	global $FilePath;
	
	// A folder for custom files is manually added to the appropriate location
	$BaseCustomDir = get_stylesheet_directory()."/Custom";
	// The post id is used to correctly identify the files to include
	$CurID = get_the_ID(); 
	$FilePath = $BaseCustomDir."/custom-css/custom-css-".$CurID.".css";

	// Only want to include custom content when a single post is being shown 
	if( is_singular() )
	{
		
		// Check for custom CSS files. Link if file exists.
		if(file_exists ( $FilePath))
		{
			$RelFilePath = get_stylesheet_directory_uri()."/Custom/custom-css/custom-css-".$CurID.".css";
			// Custom CSS file exists so it is included 
			?>
			<!-- Custom CSS via plugin -->
			<link rel="stylesheet" type="text/css" href="<?php echo $RelFilePath; ?>" />
			<?php
		}
		
		// Check for custom content. Link if file exists. Filename must be exact. 
		$FilePath = $BaseCustomDir."/Custom-content/custom-content-".$CurID.".php";
		//error_log("FilePath before insert content function: ".$FilePath);
		if(file_exists ( $FilePath))
		{
			//error_log("FilePath for content: ".$FilePath);
			add_filter('the_content', 'InsertCustomContent');	
		}
		
	}
	
};

My intent is to make adjustments to allow similar files to be included such as scripts and also to allow control over where the content is placed.

Leave a Reply

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

Category:Wordpress
Pagename: wordpress - Wordpress

WordPress – Useful links – Reference info

WordPress template hierarchy – How wordpress decides which template to show based on user selections. Thumbnail goes to flowchart, link goes to ‘basics’ page.
wpdb – wpdb class controls the connection and information to and from the wordpress DB (or other connected DB’s).
WP_Query – WordPress wp_query class allows manipulation of the DB query (ie change categories, authors etc)

Leave a Reply

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

Category:Wordpress
Pagename: wordpress - Wordpress

WordPress – Controlling content within the loop

When the content to be shown needs to be modified, the query used by the ‘loop’ is adjusted to suit. For example, if it was desirable to have only a specific category on the home page…

Do not use ‘query_posts‘. Apart from an increase in load time, it is known to cause problems in plugins, pagination and elsewhere.

Best practice in most situations is to write a function to adjust the main query and then attach that function to the ‘pre_get_posts‘ hook. For example:

add_action('pre_get_posts','list_solutions_only_query');
function list_solutions_only_query($query) {
	// Check the correct query/loop is being modified
	if ( $query -&gt; is_home() &amp;&amp; $query -&gt; is_main_query()  )
	{
 		// Category ids to remove: blog-22, Obsolete-31
		$query-&gt;set( 'cat', '-22,-31' );
	}
		//we remove the actions hooked on the '__after_loop' (post navigation)
		remove_all_actions ( '__after_loop');	
}

Note: Many loops/queries can be used in a typical WordPress site so need to determine the correct query is being modified by adding conditions to check for suitability.

Leave a Reply

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