Skip to content

Instantly share code, notes, and snippets.

View someguy9's full-sized avatar
🏠
Working from home

Andy Feliciotti someguy9

🏠
Working from home
View GitHub Profile
<?php
// Insert post programmatically
$new_post = array(
'post_title' => 'My new post',
'post_content' => 'Content to insert.',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $new_post );
@someguy9
someguy9 / wordpress-insert-custom-post.php
Last active February 14, 2023 17:46
Insert a custom post type programmatically in WordPress https://smartwp.com/wordpress-insert-post-programmatically/
<?php
// Insert custom post programmatically
$new_post = array(
'post_title' => 'My new example post',
'post_content' => 'My new content!',
'post_status' => 'public',
'post_type' => 'my_post_type'
);
$post_id = wp_insert_post( $new_post );
<?php
// Update a post programmatically
$my_post_id = 15
$update_post = array(
'ID' => $my_post_id,
'post_title' => 'My new post title',
'post_content' => 'Overwrite post content',
'post_status' => 'public'
);
<?php
// Overwrite MightyShare title font-weight
function mightyshare_set_font_weight( $template_options ) {
$template_options['title_fontWeight'] = '900';
return $template_options;
}
add_filter( 'mightyshare_filter_post', 'mightyshare_set_font_weight', 10, 3 );
<?php
// Overwrite MightyShare fallback image
function mightyshare_overwrite_fallback_image( $template_options ) {
var_dump(get_post_meta( $template_options['ID'], 'mightyshare_enabled', true ));
if ( get_post_thumbnail_id($template_options['ID']) !== $template_options['background'] && empty( get_post_meta( $template_options['ID'], 'mightyshare_background', true ) ) ) {
$template_options['background'] = 'https://images.unsplash.com/photo-1674757273875-e47dbfbda82c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2831&q=80';
}
return $template_options;
}
<body style="margin:0;padding:0;height:100vh;width:100vw;">
<div style="display:flex;align-items:center;height:100%;width:100%;justify-content:center;background:url('{{background}}')">
<h1 style="font-size:3.2em;color:#fff;text-shadow:0 8px 8px rgba(0,0,0,0.3)">{{title}}</h1>
</div>
</body>
@someguy9
someguy9 / rankmath-mightyshare-title.php
Last active January 21, 2023 22:41
Use RankMath title for MightyShare.io renders in WordPress https://mightyshare.io/
<?php
// If RankMath is active & a meta title is set use it with MightyShare instead of post title
function mightyshare_use_rankmath_seo_title( $template_options ) {
if ( in_array( 'seo-by-rank-math/rank-math.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
global $post;
$rankmath_title = RankMath\Post::get_meta( 'title', $post->ID );
if(!empty($rankmath_title)){
// Use RankMath title for MightyShare
$template_options['title'] = $rankmath_title;
}
<?php
// Get the current post or page's slug
$slug = get_post_field( 'post_name', get_post() );
echo $slug;
// Get slug for a post or page by ID
$post_id = 1;
echo get_post_field( 'post_name', $post_id );
@someguy9
someguy9 / wordpress-post-slug.php
Created October 31, 2022 16:23
Get the current post slug in WordPress
<?php
global $post;
$slug = $post->post_name;