Categories
Uncategorized

How to Setup WP-CLI on WordPress Server

Setting up WP-CLI is super easy and can be done in just a few steps.

Installation

First download WP-CLI using curl or wget.

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Next check if it worked.

php wp-cli.phar --info 

To make things easier, make the wp-cli.phar file an executable.

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

sudo: command not found

If you don’t have sudo powers there are a number of things you can do:

Easy Way

You can use the relative path to the wp-cli.phar file.

~/wp-cli.phar

Best Way

A better solution would be to create an alias to run the same command as above with less keystrokes.

Open up your .bashrc file and add this line to the bottom

# User specific aliases and functions
alias wp="~/wp-cli.phar"

Re-source your .bash_profile and run wp --info.

source .bash_profile
wp --info

You did it!

Categories
Ruby on Rails

How to add Bootstrap 4 to your Ruby on Rails Application

Luckily adding Twitter Bootstrap 4 to your ruby on rails application is not too complex.

Here’s the quick and dirty steps to adding bootstrap to your rails application.

Launch your app

Move into your existing application directory or run rails new myapp to create a new application.

Add the bootstrap and jquery gems to your Gemfile.

gem 'bootstrap', '~> 4.4.1'
gem 'jquery-rails'

Run bundle install --verbose (it may take a while).

Restart your server if you have it running. This is an important step for ensuring bootstrap is available in the applications pipeline.

Import styles in application.scss

Rename the default app/assets/stylesheets/application.css to a .scss file.

mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss

Remove all the *= and *= require_tree statements to have access to Bootstrap mixins and variables.

Importing bootstrap at app/assests/stylesheets/application.scss

Add jQuery

Bootstrap 4 relies on jQuery for all the JavaScript heavy lifting.

Earlier we added the ‘jquery-rails gem’ and ran bundle install.

Now all we have to do is add bootstrap dependencies to our app/assests/javascripts/application.js

//= require jquery3
//= require popper
//= require bootstrap
Add bootstrap dependencies to your application.js file.

Check the source code

To test that bootstrap was added to our applications assets pipeline, launch the server and check the source code.

Checking the applications source code for links to bootstrap.

You did it!

What did we do?

  • Added the Bootstrap and Jquery gems to our Gemfile and ran bundle install --verbose.
  • Renamed our application.css to a .scss file and added @import "bootstrap";
  • Added jQuery dependancies to application.js
  • Confirmed what we did by launching the app and checking the source code.
Categories
Ruby on Rails

How to setup Rails App for Heroku

If you want to host your Ruby on Rails application with Heroku- your database must be PostgreSQL.

Unfortantely, it can be difficult to get setup if you’ve never done anything like use the Heroku CLI or manually create databases in psql.

Here are the quick and dirty steps for setting up your rails application for hostign on heroku.

Install Heroku

If you haven’t installed heroku-cli, that is step number one.

Visit the download instructions to figure out how to install heroku.

For me (xubuntu) I used snap.

sudo snap install --classic heroku

Next, you’ll be prompted to setup/login with Heroku.

Create the application

If you are starting from scratch, run the rails new command and specify you want a postgreSQL database (since thats what heroku requires).

rails new myapp –database=postgresql

Move into your new application and run bundle install.

cd myapp
bundle install
...
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.

Test the databases

For my setup, I have to manually create the databases in PostgreSQL.

Run the following commands, using your application’s name instead of myapp.

psql
CREATE DATABASE myapp_development;
CREATE DATABASE myapp_test;
\list;

If psql didn’t work or needs to be setup, I wrote a short post about getting setup with PostgreSQL.

Go back into your application and run rails db:migrate to test your databases were added correctly.

No error message? Good!

Create a welcome page

Rails 5.x no longer includes a default page in a production environment.

So we will have to make one! First create a controller.

rails g controller pages home

Now create a home.html.erb file in the app/views/pages/ directory.

Add some simple HTML to the homepage.

<h1>Rails!</h1>
<a href="http://gerrg.com">GERRG.com Learn to build a Rails Application and Host with Heroku</a>

Finally add the route to the newly create homepage. Open up config/routes.rb and add the following line.

root 'pages#home'

Open up your terminal and launch your server with rails server.

Add it to Github

Now that you’ve got your self a fancy new rails application complete with default page in production environments…

Its time to add it to GitHub before we host the application on Heroku.

Setup repository on GitHub: add, commit and push application.

git add -A
git commit -n 'init commit'
git push

Your push will not work if you haven’t setup your remote origin. In fact, you’ll get an error message about it.

Host on Heroku

Okay so far we have:

  • created our rails application
  • setup our postgreSQL databases
  • built a default page for our production environment
  • and created a repository on github.

Finally, we are going to host our rails application with Heroku and have our site out there on the world wide web.

Run Heroku create to create a repository with Heroku, then push your application from git to Heroku.

heroku create
git push heroku master
This push will take a while and you will see lots of scary messages… just let it finish.

If the command completes, the push was complete and you should be able to visit your application with heroku open.

you did it!

Categories
Ruby on Rails

How to fix “A server is already running.” error in Ruby on Rails

Luckily this is an easy fix. You error message will more accurately say something along the lines of:

A server is already running. Check /home/greg/odin/spitter/tmp/pids/server.pid.

If you run cat on the file referenced in the message, it will return a 5 digit number like 30351.

cat /home/greg/odin/spitter/tmp/pids/server.pid returns the ID of the process.

30351 is the ID of the process running the current rails server. Run kill -9 30351 to end the process and free up room for a new server.

You are now free to run rails s to boot up the server.

Categories
Random

How to Match & Index with Excel & LibreOffice Calc

Match and index is a super useful tool that has personally saved me hours of manual copy and pasting of information.

Instead there is a better way. All you need is two sets of data, and a point where they match.

In my experience, I’ve used this to match two price lists based on SKU and easily copy over UPC codes, weight, our cost and other useful information you may want to upload to your wordpress database or whatever.

Here is a brief example of how to do this- it works on Microsoft Excel and LibreOffice Calc.

Essentially, we are going to use the MATCH and INDEX functions common on most spreadsheet programs to copy over some data (like price) based on a match in the same row (like SKU).

INDEX(the column you want to copy, MATCH(the cell you want to match, the column of data you are matching against))

Keep in mind

Match sure all data is formatted the same way; strings will not match numbers.

Make sure you make the row absolute (A$5).

Categories
Ruby on Rails

How to create PostgreSQL database for Ruby On Rails

This is a note to myself and those who are interested.

GERRG

I use Heroku for hosting my Ruby on Rails applications. Problem is, Heroku doesn’t support SQlite databases.

If you’ve ever tried to run rails db:migrate to create a postgesSQL database WITHOUT first setting up the databases manually, you will recognize this error:

rails aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "spitter_development" does not exist

Setup a PostgreSQL database

Run the following command.

psql

If you get this error, your package manager did not setup your user account up properly.

createdb: database creation failed: ERROR:  source database "template1" is being accessed by other users

Run the createdb command.

$ createdb
$ psql
psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.

Create the databases

Once you are in psql, run \list to see all the databases on your server (service)

before we add the databases

Now run the CREATE DATABASE command followed by the name of your rails applications and finally suffixed with _development and _test respectively.

Dont forget the semi-colons! ( ; )

CREATE DATABASE yourapp_development;
CREATE DATABASE yourapp_test;

Run the \list command again to confirm your databases were added correctly.

with new databases added
Categories
Wordpress

How to create a WordPress plugin

Creating the plugin

WordPress plugins make it easy for anyone to modify a WordPress website.

First, create the folder ‘gerrg-wordpress-plugin‘.

Next create the main PHP file ‘gerrg-wordpress-plugin.php‘.

cd ~
mkdir gerrg-wordpress-plugin
touch gerrg-wordpress-plugin/gerrg-wordpress-plugin.php

Next open up the .php file you just created with your favorite code editor ( I like VS Code ).

code gerrg-wordpress-plugin/gerrg-wordpress-plugin.php

File Headers

The file header contains all the plugin information like: name, description, author and licensing.

Copy the following into your gerrg-wordpress-plugin.php file:

<?php
/**
 * Plugin Name:       Gerrg WordPress Plugin
 * Plugin URI:        http://gerrg.com/how-to-create-a-wordpress-plugin/
 * Description:       Allows the admin to implement BOGO marketing rules
 * Version:           0.5
 * Author:            Gerrg
 * Author URI:        http://gerrg.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       gerrg
 * Domain Path:       /languages
 */

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

Testing the plugin

Before we get started, lets actually make sure that our plugin is setup properly.

First we are going to add some code to the plugin which simply outputs ‘Hello’. Next we will move the folder to the wp-content/plugins directory. Finally, we will activate our plugin and check for the ‘Hello’ on our webpage.

echo ‘hello’

First add the following code to the bottom of your .php file.

...
add_action( 'wp_head', 'gerrg_say_hello' );

function gerrg_say_hello(){
    echo 'hello';
}

If you are not familiar with the WordPress Plugin API, in the code above we are simply adding an action to a hook. A action is simply a function, a hook is a SPOT in the code where that function will run.

Above we are adding our gerrg_say_hello function to the wp_head hook. The wp_head hook is triggered between the <head></head> section of the website.

Simply put, we expect to see ‘hello’ at the top of our webpage.

Activating your plugin

It’s very likely you already know how to activate a plugin. First we need to move our folder into the wp-content/plugins directory of our web server.

There is more than one way to do this and I leave it to you get accomplish this task.

mv gerrg-wordpress-plugin ~/www/wp-content/plugins/

The above code is a simple linux command for moving my plugin folder into the wp-content/plugins directory of my web server.

After moving the plugin, log into your admin section of your WordPress website. Navigate to the plugin section you should see the Gerrg Woocommerce BOGO plugin.

Plugin section featuring the plugin we just created…

Click activate and navigate the the homepage. In the top left corner of the screen you should see a simple ‘hello’.

If you see the ‘hello’ message than your plugin is ready to actually do something useful.

Congrats! You’ve created your very own plugin.