PHP4WP4 – Working in WordPress
The function file
Editing function.php file inside the active theme folder on wordpress is the easiest way to make changes on the site.
Downside of using functions.php
- Your changes could get overwritten on update.
- Copying the theme and modifying it is easiest, but comes with security risks. As it won’t update.
The solution is using the child theme.
Course on Linkedin Learning: WordPress: Building Child Themes.
Copying Hello Dolly
Follow the video.
WP_Query
- Class
- A code definition or template for creating objects.
- Objects
- A group of variables and functions marked by a single identifier
Simple personal class
Class Person { ... }
$joe = new Person( 'Joe Casabona', '34' );
$phil = new Person( 'Phil Casabona', '33');
$joe->print_name();
$joe->pring_age();
Person class replace arrays.
WP_Query use to get information from the database.
$the_query = new WP_Query( $args );
$args above is an array.
Hooks: Actions and filters
- Hooks
- Allow us to insert our own code with WordPress without modifying “core”
- Types of hooks
- Actions
- Changes how WordPress does something.
- Imagine adding air conditioning to your house.
- Filters
- Modifies the information WordPress retrieves
- Imagine painnnnnting your house.
- Actions
To add action we have to use the add_action function. The example below will rung wporg_callback() when the init hook is executed.
function wporg_callback() {
//do something
}
add_action('init', 'wporg_callback');
Actions says, whenever WordPress does this specific thing, run my function.
To add filters, use add_filter function.
function wporg_filter_title($title) {
return 'The' . $title. 'was filtered';
}
add_filter('the_title', 'wporg_filter_title');
Filter is some piece of content that you want to change.
Filter says, take this infromation you have and change it in some way.
Let’s add a filter, to add a text, Captain’s log before the title of the blog.
function cap_log( $title ){
return 'Captain\'s Log: ' . $title;
}
add_filter( 'the_title', 'cap_log');
Add the above code into functions.php