Skip to content

Instantly share code, notes, and snippets.

@gatespace
gatespace / gist:f3e0339364a637eb24b25aaac0926e37
Last active April 8, 2022 13:11
ogpとtwitter card画像が登録されていない場合はデフォルトに登録した画像で上書き
<?php
//ogpとtwitter card画像が登録されていない場合はデフォルトに登録した画像で上書き
function override_ogp_img($img){
$og_image_flag = true;
if ( is_single()) {
$post_id = get_the_id();
$og_image = get_post_meta( $post_id, '_yoast_wpseo_opengraph-image', true ); // yoast 取得
if ( empty( $og_image ) ) { // yoastの投稿の設定がない
$og_image_flag = false;
}
@gatespace
gatespace / exclude-algolia-search.php
Last active January 14, 2022 08:39
WordPress + Algoliaで、Algoliaの検索対象から特定の投稿(固定ページ)を除外するプラグイン
<?php
/*
Plugin Name: Exclude Algolia search
Plugin URI:
Description: Algolia の検索結果から除外するフラグを追加。WP Search with Algolia プラグインが必要。
Version: 0.1
Author: gatespace
Author URI: https://gatespace.jp
License: GPLv2 or later
*/
@gatespace
gatespace / my_search_paginate_links.php
Created March 26, 2019 01:28
WordPressの検索結果ページ+ページネーション(WP PageNavi含む)のリンクで `/page/数字` をやめ、 `?s=word&paged=2` のリンクにする
@gatespace
gatespace / manage_posts_columns.php
Created March 22, 2019 08:20
[WordPress] 初回の記事公開日をカスタムフィールドに残す via. https://qiita.com/gatespace/items/a8853c96f892426bfabe
add_filter( 'manage_posts_columns' , 'add_publish_date_columns' );
function add_publish_date_columns( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_display_name ) {
if ( $column_name == 'date' ) {
$new_columns['publish_date'] = '掲載日時';
}
$new_columns[ $column_name ] = $column_display_name;
}
@gatespace
gatespace / file0.txt
Created March 27, 2018 12:17
WordPress ローカルサーバー〜テーマ・プラグイン構築 オレオレ環境 2018年版 ref: https://qiita.com/gatespace/items/f540451d3b8706dafed8
$ wp scaffold _s <slug>
@gatespace
gatespace / my_wpbs_draft_to_publish_update_post.php
Last active April 6, 2018 11:11
[WordPress] WP Post Branches で更新された元記事の公開日時を変更しない ref: https://qiita.com/gatespace/items/19232316662afdd04e35
add_filter( 'wpbs_draft_to_publish_update_post', 'my_wpbs_draft_to_publish_update_post', 10, 1 );
function my_wpbs_draft_to_publish_update_post( $new ) {
// 元記事の更新日時をブランチ記事の公開日時にする
$new['post_modified'] = $new['post_date'];
$new['post_modified_gmt'] = $new['post_date_gmt'];
// 元記事の公開日時は変更しない
$old_id = $new['ID'];
$old_post = get_post( $old_id );
@gatespace
gatespace / file0.php
Last active March 27, 2018 06:44
[WordPress] WP Simple Related Posts で関連記事にアイキャッチ画像や抜粋をつけたい ref: https://qiita.com/gatespace/items/705e05a1cdc1610addde
<?php
$ids = sirp_get_related_posts_id();
@gatespace
gatespace / my_get_terms.php
Last active April 25, 2018 10:47
[WordPress] ターム(カテゴリー・タグ)の記事が指定件数以下なら一覧などに出力しない ref: https://qiita.com/gatespace/items/efc3792e00e55b50b53c
<?php
// get_terms でタグに所属する投稿が指定した件数より少なかったら表示しない
add_filter( 'get_terms', 'my_get_terms', 10, 4 );
function my_get_terms( $terms, $taxonomies, $args, $term_query ) {
if ( is_admin() ) { // 管理画面だったら何もしない
return $terms;
}
if ( $taxonomies[0] != 'post_tag' ) { // 条件分岐はよしなに
return $terms;
}
@gatespace
gatespace / tag_archive_template_redirect.php
Last active January 31, 2018 07:05
[WordPress] ターム(カテゴリー・タグ)アーカイブで記事が指定件数以下なら404扱いにしたい ref: https://qiita.com/gatespace/items/a29b0814a308fa2d77d7
<?php
add_action( 'template_redirect', 'tag_archive_template_redirect' );
function tag_archive_template_redirect() {
if ( ! is_tag() ) { // タグアーカイブ以外は何もしない
return;
}
$queried = get_queried_object();
if ( $queried->count < 6 ) { // 総記事数が5件以下なら
@gatespace
gatespace / file0.php
Last active January 11, 2018 01:30
[WordPress] タグアーカイブのパーマリンクをタグIDにする ref: https://qiita.com/gatespace/items/4548fd0b9540f46b86b1
<?php
function numeric_term_link( $url, $term, $taxonomy ) {
global $wp_rewrite;
// post_tag only
if ( $taxonomy == 'post_tag' ) {
if ( $wp_rewrite->using_permalinks() ) {
$permastruct = $wp_rewrite->get_extra_permastruct( 'post_tag' );
$permastruct = str_replace( '%post_tag%', $term->term_id, $permastruct );
$url = home_url( user_trailingslashit( $permastruct, 'tag' ) );