Author: admin

  • WordPress Template Hierarchy

    WordPress Template Hierarchy

    WordPress Template Hierarchy

    Every Template in wordpress has a corresponding php file. I have added the php files that each template uses. Below is a table with further explanations for each template.

  • PHP FUNCTIONS

    PHP FUNCTIONS

    Here is an easy-to-follow way to learn php functions. you always start the function by typing function followed by the name you want to give the function e.g. function =MyFunction() and then you enter the code you want the function to execute inside the curleybrackets{ Enter function code here}

    <?php 
    
    // FIRST YOU DEFINE A FUNCTION 
    function MyFunction() {
    	echo "Hello World!";
    }
    
    // THEN YOU "CALL" THE FUNCTION TO RUN IT 
    MyFunction();
    
    // CREATE A FUNCTION THAT TAKES IN AN ARGUMENT
    function MyFunction2($product) { //$products is a argument. 
    	echo "The price for $product is $1"; // use the argument inside echo.
    }
    //CALL THE FUNCTION 
    MyFunction2('Advocado'); // When you run the function pass in a value of your choice e.g "advocado" as argument. The function then Echoes "The price for Advocado is $1";
    # MyFunction2(); // echoes Error, beucase it expects an Argument, but you can create a default for the argument 
    
    
    //CREATE A FUNCTION THAT TAKES 2 ARGUMENTS 
    function TwoArguments1($option1,$option2) { //Two arguments
    	echo "Option1: $option1 and Option2: $option2"; 
    }
    
    //CALL THE FUNCTION
    TwoArguments1('Blue',"red"); //This wil echo Option 1: Blue and Option 2: red
    
    
    
    //CREATE DEFAULT VALUE FOR ARGUMENT $PRODICTS - IN CASE NO ARGUMENT IS PASSED INTO FUNCTION
    
    function MyFunction3($products = "Advocado") {
    	echo "The price for $products is $1" ;
    }
    
    MyFunction3(); // It echoes "The price for Advoacdo is $1" ; 
    // Advocado is the default value. If you didn't have a default value, it would give an "error message". 
    
    
    // WRITE FUNCTION THAT TAKES IN ASSOCIATIVE ARRAY
    function ChangeItem($item) {
    	echo "The {$item['name']} is free"; // if a variable is an array it must be placed {} curley brackets when echoing it. 
    	echo "The {$item['name']} is {$item['color']} <br />"; // you need to pt {$variable['name']} inside curley brackets for the echo function if you want to pull out an index [0] or value ['name'] from a $variable - the value represents an item in an array. 
    	}
    
    // CALL THE FUNCTION: PASS IN AN ASSOCIATIVE ARRAY TO THE ABOVE FUNCTION. 
    ChangeItem(['name' => 'Balloon','color'=> 'blue']);
    
    
    
    
    // WRITE A FUNCTION THAT TAKES IN 2 ASSOCIATIVE ARRAYS AS ARGUMENTS 
    function TwoArrays($itemX = 0, $properties = "no property selected") {
        echo "<br /> Item name: {$itemX['name']} and Item Price: $ {$itemX['price']}";
        echo "<br /> Color: {$properties['color']} and Weight: {$properties['weight']}";
    }
    // CALL THE FUNCTION. 
    TwoArrays(
        ['name' => 'Kiwi', 'price' => '20'],
        ['color' => 'Blue', 'weight' => '20']
    // EACH ARRAY MUST BE PLACED IN [] e.g. function([array1],[array2]);
    );
    
    
    
    
    // USING RETURN IN FUNCTIONS
    function ChangeItem2($item2) {
    return "The {$item2['name']} is {$item2['color']} <br />";
    // This returns the value so you can store it in a variable when calling the function.  
    } 
    $Returned_value = ChangeItem2(['name' => 'Ballon','color'=> 'red']); 
    echo $Returned_value; 
    
    
    
    
    
    //FUNCTION WITH 2 ARGUMENTS ( 2 ARRAYS as arguments in this example)
    function TwoArguments($height = 0, $width = 0) {
    return "Height: {$height['height']} and Width: {$width['width']}";
    }
    
    $Returned_value2 = TwoArguments(['height'=> '10'],['width'=> '12']);
    echo $Returned_value2;
    
    
    ?>
    
    
  • PHP LOOPS: For, Foreach & While

    PHP LOOPS: For, Foreach & While

    PHP for beginners

    For loops

    This is a for loop

    <?php
    // FOR LOOP
    for($i = 0; $i<4; $i++) {
    	echo $i;
    }
    ?> 


    Let’s break it down
    $i=0;
    start at the value 0
    $i<4; run the loop as long as $i is less than 4
    $i++ Increment $i by 1 every time you run through the loop
    This loop will run 4 times because $i starts with the value 0, then after the first loop it’s 1, after the second loop 2, then 3 then 4. When $i > 4 the loop will stop.

    This loop will echo the value of $1 for each loop so it will echo “0123” and then it stops.

    For loop: Blogpost example

    Instead of $i<5;
    Try $i <count($blogposts);
    Now it will keep running through each blogpost and stop after post4

    <?php 
    // FOR LOOP
    $blogposts=['post1','post2','post3','post4'];
    for($i = 0; $i <count($blogposts); $i++) {
    	echo $i;
    	echo " --- ";
    	echo $blogposts[$i]." --- ";
    } 
    // This will echo 0 --- post1 --- 1 --- post2 --- 2 --- post3 --- 3 --- post4 ---
    ?> 

    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post; 

    Foreach loop

    How the foreach loop works (look at example below)
    It’s especially used used when you don’t know the number of items in an array
    (you don’t set $i in this loop) .
    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post;

     

    <?php 
    
    $blogposts=['post1','post2','post3','post4'];
    //FOREACH LOOP 
    foreach($blogposts as $post) {
    	echo $post; 
    // This echoes out post1post2post3post4
    // Try echo $post . '<br />'; to add linebreak between each loop. 
    }
    ?> 

    A foreach loop is easier when cycling through an array than the for() loop as you need to write less code. With the for loop you can specify how many loops you want to run, you cannot do that with the foreach loop.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    //FOREACH LOOP 
    foreach($products as $product) {
    echo $product['name'] . ' - '. $product['price'];
    echo '<br />';
    }
    // ECHOES OUT product name and price of each product 
    
    ?> 

    While loop 

    $i=0 This must be declared outside the while loop function
    count($products) This will be the number 7, because we have 7 products in the array.
    while($i < count($products)) is the same as writing while(0 < 7) so the loop will run 7 times.
    $i++ Means increment $i by one each time the loop runs: you must increment $i inside the loop function or else the loop will never stop (infinite loop) because 7 is always less than zero.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    
    //WHILE LOOP
    $i = 0; // $i must be delared outside the loop. 
    
    while($i < count($products)) {
    	echo $products[$i]['name'];
    	echo' <br />'; 
    	$i++; // you must increment i or else it's always =0. 
    //count($products) is = 7 (there are 7 products in the array)
    }
    ?>

  • MySQL Commands

    MySQL Commands

    MySQL Commands table

    In this example we create a Database Called Movies. So every time you see the word “Movies” it’s representing a Database.

    The first steps are to create a database and then select the database you created.

    1. Create a Database: CREATE DATABASE MOVIES;
    2. Chose the database you want to perform operation on: USE MOVIES;
    3. Check that MOVIES is now the selected Database: SELECT DATABASE(); (it should display movies)
    4. Now you are ready to perform operations on the Database.
    SUBJECT MYSQL COMMAND + EXAMPLE
    Create DB CREATE DATABASE MOVIES;
    Drop (delete) DBDROP DATABASE MOVIES;
    Show all DBSHOW DATABASES;
    use specific DB USE MOVIES
    # Selects a specific database
    Select the used databaseSELECT DATABASE();
    If you typed USE MOVIES, it will return that database. It returns the Database you decided to “USE”. When you type commands, they will apply to the database you use.
    Create a Table
    & insert Values into the table
    CREATE TABLE MOVIES (
    col 1,
    col 2,
    col 3
    col x
    )


    CREATE TABLE MOVIES (
        movie_titles VARCHAR(60),
        imdb_ratings INT
    year_release INT,
    genre VARCHAR(30),
    );

    INSERT INTO MOVIES (
    id INT PRIMARY KEY AUTO_INCREMENT,
    movie_titles,
    imdb_ratings,
    year_release,
    genre

    )
    VALUES ('WALL-E', 6,2008,'Animation'),
    ('Titanic',8, 1997, 'Historical'),
    ("Top Gun". 6, 1987, "action")



    INSERT DATA INTO TABLEWhen inserting data into a table you write 2 functions: First you specify which columns (col_name) you want to insert data into. Then below you Specify which Values you want to insert into those columns)

    INSERT INTO MOVIES (
    id INT PRIMARY KEY AUTO_INCREMENT,
    movie_titles,
    imdb_ratings,
    year_release,
    genre

    )
    VALUES ('WALL-E', 6,2008,'Animation'),
    ('Titanic',8, 1997, 'Historical'),
    ("Top Gun". 6, 1987, "action")

    Show Columns from a TABLE_NAME. Show all columns from movies table
    SHOW COLUMNS FROM MOVIES;

    This does the same as above
    DESC MOVIES;
    DESCRIBE MOVIES;
    Select sth from a tableSELECT movie_titles FROM movies;

    SELECT * FROM movies;

    SELECT movie_titles FROM movies WHERE imdb_ratings > 6;

    SEÆECT movie_titles, imdb_ratings, year_release FROM movies WHERE imdb_ratings > 6;
    SELECT EVERYTHING WHERE
    ALIAS: select a column from table and give the column header an alias when you output, SELECT movie_title AS movie_name FROM MOVIES;
    So this selects the column called “movie_title” but in the displayed result the column header gets named “movie_name” instead of “movie_title”

    EXAMPLE 2
    SELECT id AS movie_id FROM MOVIES;

    EXAMPLE 3
    SELECT imdb_ratings AS movie_ratings, title AS movie_name FROM MOVIES WHERE imdb_ratings <6;
    DROP TABLE TABLE_NAMEDeletes the table
    DROP TABLE MOVIES;
    Update a table Update the table called Movies

    UPDATE MOVIES
    SET imdb_ratings = 8
    WHERE movie_titles = “WALL-E”

    NOTE! Make sure you didn’t leave an empty space in the name when you inserted it into the TABLE. If you did, you have to write “WALL-E ” instead of “WALL-E”

    Example: Update password for users
    UPDATE users
    SET password = “New_Password”
    Where ID = 102;

    (ID is better than name, because 2 people could have the same name, but each user only has only 1 unique ID)
    Delete DELETE FROM movies
    WHERE id = 1

    This deletes the movie whose id = 1.

    DELETE * FROM movies
    This deletes the entire table (careful!)
    CHAR(10) vs VARCHAR(10) If you create a table
    CREATE TABLE users (
    name CHAR(10)
    username VARCHAR(10)
    )
    and you insert the values into

    INSERT INTO users(name, username)
    VALUES (“Thom”,”Thom123″);

    VARCHAR(10) means “up to 10 characters”
    CHAR(10) means “exactly 10 characters” so if a user types a username less that 10 characters e.g Thom123 it will be stored with 3 blank spaces “Thom123 “
    So use VARCHAR when possible!
    TIMESTAMPS If a user makes a comment or updates the comment, you need to set a timestamp for when the comment was created and when it was updated
    CREATE TABLE user_comments (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(25),
      comment_content VARCHAR(255),
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    );

    So the Default value for the Timestamps is the Current time.

    Table will be updated.

    REPLACE SELECT REPLACE (“Big dog”, “Big”, “Small”

    Result
    “Small dog”

    SELECT REPLACE(username, “Willie”, “James”) FROM USERS;

    This replaces any username that is Willie, with James in the table USERS.
  • WordPress Gutenberg Block editor – Query loop – Mobile Friendly queries.

    WordPress Gutenberg Block editor – Query loop – Mobile Friendly queries.

    WordPress Gutenberg Block editor – Query loop – Mobile Friendly queries.

    How to use the query loop: You can display a query loop either as Vertical List or as a Grid.

    Query loop as a Grid

    1. Select the Post Template and choose “grid”
    2. Select the Grid in list view > style > choose minimum Height 100%: Now every grid-box that contains a post entry will span the full height of its container (if you set a background color to the grid, you will see that the background fills the whole grid)
      • Set Grid item Position to Auto. If you set it to manual choose Columns 1.
    3. Optional: Add a stack inside the Grid (so grid is parent): Then place Title, Featured image and Date inside stack.
      • Now set stack Justification to “Stretch items ” It makes all child blocks inside the Stack stretch to the full available width of the container. Optionally you could also choose “center”
      • Set orientation: Vertical
      • Stack Layout : Justification Grid item position
    4. Below I’ve created a border around the Grid (outer border) and the Stack (inner border): Here is the list view Structure
    5. Center the text
    query loop

    This is how the Query loop result will look on your page

  • WordPress Gutenberg Block editor – Columns, Stacks and Groups

    How to build a 3 column layout with gutenberg Block editor that is mobile-friendly.

    When you build a 3-column layout you have 3 columns within a parent column container. Notice that I gave each column a border. Columns don’t give you the option to stretch the column to take up 100% height of its parent container, so if you look at the image below you can see that column 1 is not taking up the full space of its parent container. Only stacks allow this, option, not columns. Also If you place the blocks (Header, image, paragraph, button) within each Column. You cannot push the button to the bottom if

    Solution Add a stack

    1. You have 3 columns winin a parent column container. 
      • Set outermost parent column to “Stack on mobile” This way each column will stack on top of the other on a mobile device instead of having each column side by side.
    2. within each column you have 1 stack.  
      1. Stack settings:
        1.  Change vertical alignment to “space between” 
    3. Within the stack you have 2 groups
      1. Button group
        1. Justify Center: 
        2. The button group: Set bottom margin which lifts it up from the bottom.

    Col1 GroupA

    Paragraph text col 1 groupA

    Col2 GroupC

    Paragraph text col 2 Group C . Contains an extra line.

    Col3 Group E

    Paragraph text col 3 stack E.. This has two or three extra lines taking up more space.

    That’s how you use Columns and Stacks to create evenly spaced buttons / other block elements inside a column container.