Skip to content

Instantly share code, notes, and snippets.

View atomjoy's full-sized avatar

Atomjoy atomjoy

View GitHub Profile
@atomjoy
atomjoy / Custom-db-table.php
Last active September 15, 2024 19:26
Wordpress custon database table.
<?php
function wp_setup_translations_table(){
global $wpdb;
$table_name = $wpdb->prefix . "translations"; //get the database table prefix to create my new table
$sql = "CREATE TABLE $table_name (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
identifier varchar(255) NOT NULL,
translation varchar(255) NOT NULL,
@atomjoy
atomjoy / Custom-page-author.php
Last active September 17, 2024 19:05
Custom WordPress author page with pagination.
<!-- Author Page -->
<?php
$id = get_query_var('author');
// Author details
$avatar = get_avatar_url($id, 256);
$displayname = get_the_author_meta( 'display_name', $id );
$first_name = get_the_author_meta( 'first_name', $id );
$last_name = get_the_author_meta( 'last_name', $id );
@atomjoy
atomjoy / Post-share-social-links.php
Last active September 17, 2024 17:02
Create shareable social links for url in WordPress.
<?php
/**
* Create shareable social links for url.
*
* Fontawesome required for icons
* <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
*
* @param string $url Post url get_permalink()
* @param string $class Css class
@atomjoy
atomjoy / google.md
Last active September 17, 2024 16:24
Google app passwords and Gmail smtp server settings.
@atomjoy
atomjoy / Load-smtp-send-email.php
Last active September 23, 2024 10:49
How to set Gmail smtp settings in WordPress.
<?php
// Create 16-digit password for google account
// https://myaccount.google.com/apppasswords
// Ustawienia SMTP email: wp-config.php
define('SMTP_username', '[email protected]'); // wpisz swój adres e-mail dla WordPress
define('SMTP_password', 'twoje-haslo'); // tu podaje swoje hasło Gmail
define('SMTP_server', 'smtp.gmail.com'); // tu podaj swój host serwera poczty
define('SMTP_FROM', '[email protected]'); // wpisz swój adres e-mail dla WordPress
@atomjoy
atomjoy / Load-rewrite-route-params.php
Last active September 23, 2024 10:48
Create new rewrite routes in WordPress.
<?php
// Add new route
add_action('init', function () {
add_rewrite_tag('%wow_email%', '([a-z0-9-@.]+)'); // Update query_vars
add_rewrite_rule('confirm/email/([a-z0-9-@.]+)[/]?$', 'index.php?wow_email=$matches[1]&wow_param=confirm', 'top');
// Flush permalinks rewrites for tests only
flush_rewrite_rules();
// Flush permalinks rewrites in theme or go to WP Admin > Settings > Permalinks > Save.
// add_action('after_switch_theme', flush_rewrite_rules());
@atomjoy
atomjoy / Subscribe-to-newsletter.php
Last active September 18, 2024 12:46
Subscribe to newsletter in WordPress with confirmation emails routes and unsubscribe.
<?php
// Create database table
function wow_create_subscribers_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wow_subscribers';
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
email varchar(100) NOT NULL UNIQUE,
code varchar(50) NOT NULL UNIQUE,
@atomjoy
atomjoy / Send-phpmailer-email.php
Last active September 20, 2024 14:17
How to catch PHPMailer or wp_mail errors in WordPress.
<?php
// 1. Send email with phpmailer
function wp_send_email($email, $subject) {
require_once(ABSPATH . WPINC . '/PHPMailer/PHPMailer.php');
require_once(ABSPATH . WPINC . '/PHPMailer/SMTP.php');
require_once(ABSPATH . WPINC . '/PHPMailer/Exception.php');
try {
// Create email message here
@atomjoy
atomjoy / Load-contact-form.php
Created September 19, 2024 15:10
How to send email from contact form with javascript in WordPress.
<?php
// Catch mail error
add_action('wp_mail_failed', function ($error) {
wp_send_json_error('Send mail error.', 404);
});
// Force html email
add_filter( 'wp_mail_content_type', function () {
return "text/html";
@atomjoy
atomjoy / Load-wp-title.php
Last active September 23, 2024 10:47
How to dynamically change your site title in WordPress.
<?php
// Overwrite title
add_filter('wp_title', 'change_title', 100);
function change_title($title) {
global $post;
if (is_home()) {
bloginfo('name');
} else if (is_category()) {