Working With WordPress Hooks, How To Create & Use Them.

add_action( 'publish_post' 'notify_if_food', 10, 2 );function notify_if_food( $post_id, $post ) {    if ( 'food' == get_post_type( $post_id ) ) {        $to = 'owner@site.com';
$subject = 'Notification: Food Item Just Created.';
$body = 'A Food Item called ' . $post->post_title . ' was just created.';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers ); }
}
add_action( 'publish_post' 'notify_if_food', 10, 2 );
function notify_if_food( $post_id, $post ) {    if ( 'food' == get_post_type( $post_id ) ) {        $to = 'owner@site.com';
$subject = 'Notification: Food Item Just Created.';
$body = 'A Food Item called ' . $post->post_title . ' was just created.';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers ); }
}
add_action( 'admin_notices', 'independence_notice' );function independence_notice() {
global $pagenow;
$admin_pages = [ 'index.php', 'edit.php', 'plugins.php' ];
if ( in_array( $pagenow, $admin_pages ) ) {
if ( date( 'j, F' ) === '1, October' ) {
?>
<div class="notice notice-warning is-dismissible">
<p>Happy Independence Day, Nigeria...</p>
</div>
<?
}
}
}
add_action( 'comment_post', 'notify_my_mail', 10, 2 );function notify_my_mail( $comment_id, $comment_approved ) {
if ( ! $comment_approved ) {
$comment = get_comment( $comment_id );
$mail = 'you@email.com';
$subject = sprintf( 'New Comment by: %s', $comment->comment_author );
$message = $comment->comment_content;
wp_mail( $mail, $subject, $message );
}
}
add_action( 'login_head', 'my_logo' );function my_logo() {
echo '
<style type="text/css">
h1 a {
background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important;
}
</style>
';
}
add_action( 'auth_redirect', 'log_unauthorized_bots' );function log_unauthorized_bots() {
$post = wp_insert_post(
array(
'post_type' => 'logs',
'post_title' => 'Unauthorized Bot Attempt - ' . date("h:i:sa | d, F Y"),
'post_status' => 'publish',
'meta_input' => array(
'ip_address' => $_SERVER['REMOTE_ADDR'],
'attempted_url' => $_SERVER['REQUEST_URI'],
),
)
);
}
add_action( 'wocommerce_before_main_content', 'custom_woo_opening', 5 );function custom_woo_opening() {
if( ! is_product() ) return;
echo '<div class="custom-woo-styling">';
}
add_action( 'wocommerce_after_main_content', 'custom_woo_closing', 50 );function custom_woo_closing() {
if( ! is_product() ) return;
echo '</div>';
}
add_action( 'woocommerce_add_to_cart', 'add_product_to_cart', 10, 2 );function add_product_to_cart( $item_key, $product_id ) {
global $woocommerce;
$product = wc_get_product( $product_id );
$cart_item = array();
remove_action( 'woocommerce_add_to_cart', __FUNCTION__ );
$woocommerce->cart->add_to_cart( $product_to_add_id, 1, 0, array(), $cart_item );
}
add_action( 'woocommerce_check_cart_items', 'empty_cart_notice' );function empty_cart_notice() {
if ( WC()->cart->get_cart_contents_count() == 0 ) {
wc_print_notice( __( 'Your shopping cart is empty! Why?', 'woocommerce' ), 'notice' );
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store