Skip to content

Instantly share code, notes, and snippets.

@delennerd
Last active January 14, 2022 08:57
Show Gist options
  • Save delennerd/40b3259c59e4966a3981b393b59d36d7 to your computer and use it in GitHub Desktop.
Save delennerd/40b3259c59e4966a3981b393b59d36d7 to your computer and use it in GitHub Desktop.
WordPress Advanced Programming Functions - Some nice function to make your website nicer
WordPress Advanced Programming Functions - Some nice function to make your website nicer
<?php
public function logged_in_menu_items( $items, $args )
{
if ( is_user_logged_in() ) {
if ( 'primary' === $args->theme_location ) {
$output = sprintf(
'<li class="menu-item nav-item"><a href="%1$s" class="nav-link">%2$s</a></li>',
home_url( '/dashboard' ),
'<span class="mdi mdi-account-circle"></span>' . __( 'Mein Konto' ),
);
$items .= $output;
}
}
return $items;
}
<?php
define( 'MY_DIR_TEMPLATES', plugin_dir_path( __FILE__ ) . 'templates' );
class MY_Class {
function __construct()
{
add_filter( 'init', array( $this, 'generate_rewrite_rules' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
add_action( 'template_include', array( $this, 'rewrite_template' ) );
}
public function generate_rewrite_rules( )
{
add_rewrite_rule(
'custom-page/([0-9]+)[/]?$',
'index.php?pagename=custom_page_name&page_id=580&product_id=$matches[1]',
'top'
);
}
public function add_query_vars( $vars )
{
$vars[] = 'custom_page';
$vars[] = 'product_id';
return $vars;
}
// Alternate for custom page > Override a page template
public function rewrite_template( $template )
{
if ( false === get_query_var('product_id') || 'custom_page_name' !== get_query_var('pagename') ) {
return $template;
}
return MY_DIR_TEMPLATES . '/single-product.php';
}
}
<?php
define( 'MY_DIR_TEMPLATES', plugin_dir_path( __FILE__ ) . 'templates' );
class MY_Class {
function __construct()
{
add_filter( 'init', array( $this, 'generate_rewrite_rules' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
add_action( 'template_redirect', array( $this, 'rewrite_rule' ) );
}
public function generate_rewrite_rules( )
{
add_rewrite_rule(
'custom-page/([0-9]+)[/]?$',
'index.php?pagename=custom_page_name&product_id=$matches[1]',
'top'
);
// To use pagination
add_rewrite_rule(
'custom-page/page/([0-9]+)[/]?$',
'index.php?pagename=custom_page_name&paged=$matches[1]',
'top'
);
}
public function add_query_vars( $vars )
{
$vars[] = 'custom_page';
$vars[] = 'product_id';
return $vars;
}
function rewrite_rule()
{
global $werte_register_plugin;
$pagename = get_query_var( 'pagename' );
if ( 'custom_page_name' === $pagename ) {
$product = $this->get_product_data( get_query_var('product_id') );
//
$post_title = $edition_data->title;
$post_canonical = home_url( $wp->request );
add_filter( 'rank_math/frontend/title', function( $title ) use ($post_title) {
return $edition_title;
});
add_filter( 'rank_math/frontend/canonical', function( $canonical ) use ($post_canonical) {
return $post_canonical;
});
// If you get an 404 error on the page > fix with status header
status_header( 200 );
include MY_DIR_TEMPLATES . '/single-product.php';
die;
}
}
}
<?php
/**
* https://gist.github.com/pauln/884e1a229d439640fbe35e848852fe0b
*
* Bulk inserts records into a table using WPDB. All rows must contain the same keys.
* Returns number of affected (inserted) rows.
*/
static function wp_db_bulk_insert($table, $rows) {
global $wpdb;
$table = $wpdb->prefix . $table;
// Extract column list from first row of data
$columns = array_keys($rows[0]);
asort($columns);
$columnList = '`' . implode('`, `', $columns) . '`';
// Start building SQL, initialise data and placeholder arrays
$sql = "INSERT INTO `$table` ($columnList) VALUES\n";
$placeholders = array();
$data = array();
// Build placeholders for each row, and add values to data array
foreach ($rows as $row) {
ksort($row);
$rowPlaceholders = array();
foreach ($row as $key => $value) {
$data[] = $value;
$rowPlaceholders[] = is_numeric($value) ? '%d' : '%s';
}
$placeholders[] = '(' . implode(', ', $rowPlaceholders) . ')';
}
// Stitch all rows together
$sql .= implode(",\n", $placeholders);
// Run the query. Returns number of affected rows.
return $wpdb->query($wpdb->prepare($sql, $data));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment