functions.phpカスタマイズ

追加している項目です。
参考にしたサイトのURLはコメントとして記述してます。

  • 自前のグローバル変数
  • 公開になっているカスタム投稿でMarkdownが使えるようにする
  • rssにカスタム投稿も追加する、抜粋でもサムネイルを追加する
  • ブログのトップページににカスタム投稿を含める、投稿者の記事一覧にカスタム投稿を含める
  • カスタム投稿タイプでカテゴリ未選択時にデフォルトで ぼやき を設定
  • トップページに文言表示用 widget
  • simplicityのアーカイブ表示時のタイトル変更
  • prismのショートコード追加
  • WordPressコードの自動変換を停止
  • JetpackのOGPを止めて、simplicityのOGPを使う
  • Jetpackのパブリサイズの文言変更
  • Maron用 新着エントリーウイジェットの追加

functions.php

<?php //子テーマ用関数

//親skins の取得有無の設定
function include_parent_skins(){
  return true; //親skinsを含める場合はtrue、含めない場合はfalse
}

//子テーマ用のビジュアルエディタースタイルを適用
add_editor_style();

//以下にSimplicity子テーマ用の関数を書く

//****** 自前のグローバル変数 ******
$my_maron_category_default = 'moan';
// カスタム投稿タイプとアイコン
$my_post_icon = array(
                  "maron" => "fa-paw",
                );


//公開になっているカスタム投稿でMarkdownが使えるようにする
function my_custom_init() {

    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    $output = 'names'; // names or objects, note names is the default
    $operator = 'and'; // 'and' or 'or'

    $post_types = get_post_types( $args, $output, $operator ); 

    foreach ( $post_types  as $post_type ) {
        add_post_type_support( $post_type, 'wpcom-markdown' );
    }
}
add_action( 'init', 'my_custom_init' );

//rssにカスタム投稿も追加する
function mysite_feed_request($vars) {
    if ( isset( $vars['feed'] ) && !isset( $vars['post_type'] ) ) {
        $vars['post_type'] = array(
        'post',
        'maron'
        );
    }
    return $vars;
}
add_filter( 'request', 'mysite_feed_request' );

//rssが抜粋でもサムネイルを追加する。
function rss_post_thumbnail( $content) {
    global $post;
    if (has_post_thumbnail( $post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) .'</p>' . $content;
    }
    return $content;
}
add_filter( 'the_excerpt_rss',  'rss_post_thumbnail');
add_filter( 'the_content_feed', 'rss_post_thumbnail');


// pri_get_postsを使って、ホームページにカスタム投稿を含める
// 投稿者一覧にカスタム投稿を含める
// 参考サイト http://qiita.com/_ruka_/items/e14280d34eddf49efad1
function change_posts_per_page($query) {
   /* 管理画面,メインクエリに干渉しないために必須 */
   if ( is_admin() || ! $query->is_main_query() ){
      return;
   }
   /* ホームページの場合、ポストタイプにカスタム投稿を含める */
   if ( $query->is_home() ) {
      $query->set( 'post_type', array('post' ,'maron') );
      return;
   }
   /* 投稿者の記事一覧の場合、ポストタイプにカスタム投稿を含める */
   if (is_author() && $query->is_main_query() ) {
      $query->set( 'post_type', array( 'post', 'maron' ) );
  }
}
add_action( 'pre_get_posts', 'change_posts_per_page' );


// カスタム投稿タイプでカテゴリ未選択時にデフォルトで ぼやき を設定
function add_defaultcategory_automatically($post_ID) {
    global $wpdb;
    // 設定されているカスタム分類のタームを取得
    $curTerm = wp_get_object_terms($post_ID, 'm_category');
    // 既存のターム指定数が 0(つまり未設定)であれば)初期値のターム名を指定
    if (0 == count($curTerm)) {
        // 初期値のターム名からターム ID を取得して設定する
        global $my_maron_category_default;
        $term = get_term_by('slug',$my_maron_category_default , 'm_category');
        if ( !empty( $term ) ) {
           $defaultTerm = array( $term->term_id );
           wp_set_object_terms($post_ID, $defaultTerm, 'm_category');
        }
    }
}
add_action('publish_maron', 'add_defaultcategory_automatically');

// トップページに文言表示 widget ここにテキストウィジットを置く
// 参考サイト http://ryus.co.jp/blog/toppage-widget-simplicity/
if (function_exists('register_sidebar')){
    register_sidebar(array(
        'before_widget' => '<div class="top_main_widget" id="%1$s">'."\n",
        'after_widget' => '</div>'."\n",
        'before_title' => '<h1 id="archive-title">',
        'after_title' => '</h1>',
        'name' => 'トップメイン',
        'id' => 'top_main_widget'
    ));
}

// アーカイブのタイトル変更
function get_archive_chapter_text(){ 
    $chapter_text = null; //アーカイブタイトル前
    if( is_category() ) {
        $chapter_text = 'Nyankichi:';
    } elseif( is_tax() ) {
        $chapter_text = 'Maron:';
    } elseif (!is_author()) {
        if( is_post_type_archive( 'maron' ) ) {
            $chapter_text = 'Maron:';
        } else {
            $chapter_text = 'Nyankichi:';
        }
    }
    $lang = Mlp_Helpers::get_current_blog_language();
    if ( $lang == 'ja_JP' ) {
        $chapter_text = '「' . $chapter_text; //アーカイブタイトルの取得 
        $chapter_text .= get_archive_chapter_title(); //アーカイブタイトル後 
        $chapter_text .= '」一覧'; //返り値として返す 
    } else {
        $chapter_text = '[' . $chapter_text; //アーカイブタイトルの取得 
        $chapter_text .= get_archive_chapter_title(); //アーカイブタイトル後 
        $chapter_text .= '] Archive'; //返り値として返す 
    }
    return $chapter_text;
}

// ************************************************************
// ****** ここから、prismのショートコード追加 *******
// 参考サイト https://tech.cmd08.com/wordpress-prism-js-shortcode

function set_prism_js()
{
    if (is_single()) {
        wp_enqueue_style('prism-style', get_stylesheet_directory_uri(). '/prism/prism.css', false, null);
        wp_enqueue_script('prism-script', get_stylesheet_directory_uri(). '/prism/prism.js', false, null, true);
    }
}
add_action('wp_enqueue_scripts', 'set_prism_js');

function shortcode_prism_js($atts, $content = null)
{
    extract(shortcode_atts([
        'language'     => '',
        'data_language' => '',
        'data_line'     => '',
        'data_start'    => ''
    ], $atts));

    if ($data_language) {
        $data_language = ' data-language="'. esc_attr($data_language). '"';
    }

    if ($data_line) {
        $data_output = ' data-output="'. esc_attr($data_line). '"';
        $data_line = ' data-line="'. esc_attr($data_line). '"';
    }

    if ($data_start) {
        $data_start = ' data-start="'. esc_attr($data_start). '"';
    }

    if ($language == "command-line") {
       $format = '<pre class="command-line" data-user="user" data-host="host"%s><code>%s</code></pre>';
       return sprintf(
                $format,
                $data_output,
                //htmlspecialchars(trim($content), ENT_QUOTES, 'UTF-8')
                $content
            );    
    } else {    
       $format = '<pre class="line-numbers"%s%s%s><code>%s</code></pre>';
       return sprintf(
                $format,
                $data_language,
                $data_line,
                $data_start,
                esc_attr($language),
                //htmlspecialchars(trim($content), ENT_QUOTES, 'UTF-8')
                $content
            );
    }
}
//add_shortcode('prism', 'shortcode_prism_js');

add_filter('the_content', function ($content) {

    // 登録されている全ショートコード
    global $shortcode_tags;

    // 登録されているショートコードを退避してから消去
    $orig_shortcode_tags = $shortcode_tags;
    remove_all_shortcodes();

    // wpautop関数実行前に処理したいショートコードをここで登録
    add_shortcode('prism', 'shortcode_prism_js');

    $content = preg_replace_callback(
        '/\[prism(.+?)\](.+?)\[\/prism\]/su',
        function ($matches) {
            return '[prism'. $matches[1]. ']'. esc_html(trim($matches[2])). '[/prism]';
        },
        $content
    );

    // [prism]ショートコードを実行
    $content = do_shortcode($content);

    // 退避したショートコードを元に戻す
    $shortcode_tags = $orig_shortcode_tags;

    return $content;
}, 9, 1);
// ****** ここまで、prismのショートコード追加 *******
// ************************************************************

// コードの自動変換を停止
add_filter( 'run_wptexturize', '__return_false' );


// ************************************************************
// ****** ここから、Jetpackのパブリサイズのカスタマイズ *******
// 参考サイト https://blog.sus-happy.net/jetpack-publicize-custom/

//JetpackのOGPを止めて、simplicityのOGPを使う
remove_action('wp_head','jetpack_og_tags');
add_filter( 'jetpack_enable_opengraph', '__return_false' );

// パブリサイズ共有の文言を変更
function change_jetpack_publicize_content( $post_id, $post )  
{
    $POST_MESS = '_wpas_mess';
    // 投稿,下書き,スケジュール待ちのみ
    if ( !in_array( $post->post_status, array( 'publish', 'future' ) ) ) {
        return;
    }
    // カスタムメッセージのPOSTがあったら無視
    if ( !empty( $_POST['wpas_title'] ) ) {
        return;
    }
    // カスタムメッセージがある場合は無視
    if( get_post_meta( $post_id, $POST_MESS, TRUE ) ) {
        return;
    }
    // 共有する文言の成形
    if( $post->post_type == 'maron' ) {
        $publicize_custom_message = sprintf( "まろん投稿:『%s』 見てにゃ\n %s", $post->post_title, wp_get_shortlink( $post->ID ) );
    } else {
        $publicize_custom_message = sprintf( "ブログ投稿:『%s』 \n %s", $post->post_title, wp_get_shortlink( $post->ID ) );
    }
    // カスタムメッセージとして登録
    update_post_meta( $post_id, $POST_MESS, $publicize_custom_message );
    // postmetaが削除されないように$_POSTにも代入
    $_POST['wpas_title'] = $publicize_custom_message;
}
// JetPackのパブリサイズ共有のsave_postに対する処理の優先度は「20」
add_action( 'save_post', 'change_jetpack_publicize_content', 19, 2 );

// ****** ここまで、Jetpackのパブリサイズのカスタマイズ *******
// ************************************************************

functions-2.php(functions.phpへの追加です)

<?php
// 一行目はシンタックスハイライトするために追加しています。

///////////////////////////////////////////////////
// Maron用 新着エントリーウイジェットの追加
// /wp-content/themes/simplicity2/lib/widgets/new-entries.php をカスタマイズしてfunctons.phpに追加。
///////////////////////////////////////////////////
class MyMaronNewEntryWidgetItem extends WP_Widget {
  function __construct() {
    parent::__construct(
      false,
      '[S] Maron新着記事',
      array('description' => 'Maron新着記事リストを表示するSimplicityウィジェット改です。')
    );//ウイジェット名
  }

//
// 略
//

      //新着記事表示用の処理を書くところだけど
      //コード量も多く、インデントが深くなり読みづらくなるので
      //テンプレートファイル側に書く
      if ( $entry_type == 'default' ) {
        get_template_part('new-maron-entries');
      }else{
        get_template_part('new-entries-large');
      }

//
// 略
//

add_action('widgets_init', create_function('', 'return register_widget("MyMaronNewEntryWidgetItem");'));
参考 functions-2.php,functions.phpGist

Monappy: MBDQ39VHypMQwfyR8SshuHvfPNUz321F6B

Monacoinを投げる
モナゲ(tipmona)ってなに?
そもそもMonacoinってなに?

コメントを残す

メールアドレスが公開されることはありません。

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください