Quantcast
Channel: Wordpressコード | WEBPAPRIKA

WordPressの更新時(アップグレード)に聞かれるFTPをなんとかしたい。

$
0
0

レンタルサーバーだとWordpressの更新をしてもFTP情報を聞かれる事は少ないと思いますが、自前のサーバーとかだとFTP情報を聞かれます。
毎回、入力するのも面倒なので、なんとかしたいですね。

FTP情報を聞かれる原因は、ファイルのオーナーがhttpdのオーナーと違うからなので、chownでオーナーを同じにしたりしますが、もっと簡単にという事で、

// wp-config.php

/** Setup FTP Details **/
define("FTP_HOST", "FTPホスト名");
define("FTP_USER", "FTPユーザー名");
define("FTP_PASS", "FTPパスワード");

これをDB設定の下あたりに追記します。
これで更新をしてもFTP情報を聞かれることがなくなります。

あと、上のコードの代わりに

define('FS_METHOD', 'direct');

を入れてもFTP情報を聞かれることが無くなるようです。

via:How to Stop WordPress Asking for FTP details to Upgrade Plugins – Lime Canvas


[Wordprss]PressThisにカスタム投稿タイプを設定する

$
0
0
<?php
function press_this_ptype($link) {
	$post_type = 'bookmark';
	$link = str_replace('press-this.php', "press-this.php?post_type=$post_type", $link);
	//$link = str_replace('?u=', '&u=', $link);
	return $link;
}
add_filter('shortcut_link', 'press_this_ptype', 11);
?>

参考:Press This Custom Post Types

[WordPress] 記事投稿後のカテゴリーの並び順を変更しない

[WordPress] 記事内画像やアイキャッチのALTの内容を変更する

$
0
0

記事投稿時に変更するのか、記事表示時に変更するのかでコードが変わってきます。

記事表示時

/* 記事画像からaltを除く */
add_filter('the_content', 'my_img_filter');
function my_img_filter($html) {
  global $post;

  // altを削除
  $result = array();
  preg_match_all('|alt="[^"]*"|U', $html, $result);
  foreach($result[0] as $img_tag) {
    $html = str_replace($img_tag, '', $html);
  }
  $result = array();
  preg_match_all("|alt='[^']*'|U", $html, $result);
  foreach($result[0] as $img_tag) {
    $html = str_replace($img_tag, '', $html);
  }

  // altにタイトルを付加する
  $post_title = get_the_title();
  $html = preg_replace('/(<img.*?)\/>/', '$1 alt="'.esc_attr($post_title).'" />', $html);

  return $html;
}

/* アイキャッチからaltを除く */
add_filter('post_thumbnail_html', 'my_thumbnail_filter', 99, 5);
function my_thumbnail_filter($html, $post_id, $post_thumbnail_id, $size, $attr) {
  // altを削除
  $result = array();
  preg_match_all('|alt="[^"]*"|U', $html, $result);
  foreach($result[0] as $img_tag) {
    $html = str_replace($img_tag, '', $html);
  }
  $result = array();
  preg_match_all("|alt='[^']*'|U", $html, $result);
  foreach($result[0] as $img_tag) {
    $html = str_replace($img_tag, '', $html);
  }

  // altにタイトルを付加する
  $post_title = get_the_title();
  $html = preg_replace('/(<img.*?)\/>/', '$1 alt="'.esc_attr($post_title).'" />', $html);

  return $html;
}

参考:php – How do I change/modify the_post_thumbnail(); html output? – WordPress Development Stack Exchange
参考:WordPress › Support » Remove title= attribute from the_post_thumbnail


記事投稿時

function img_norightclick($html, $id, $alt, $title, $align, $size) {
  return str_replace('/>','oncontextmenu="alert(\'保存できません\');return false; />',$html);
}
add_filter('get_image_tag','img_norightclick', 10, 6);

参考:[WordPress]記事内に画像を挿入するときのHTMLをカスタマイズする | Wood-Roots.blog

[WordPress] 子テーマから親テーマのfunctions.phpを変更する

$
0
0

子テーマは親テーマを継承しますが、functions.phpに書いてある内容を変更したい、または削除したい場合があります。
同じ関数名を子テーマに書いて上書きを試みると、「Fatal error: Cannot redeclare」エラーになります。

参考:Overriding WordPress Functions | venutip.com

エラーを回避するには、親テーマでadd_actionされている関数を子テーマでremove_actionしてから別名でadd_actionします。

子テーマのfunctions.php

// Remove the default Thematic blogtitle function
function remove_thematic_actions() {
    remove_action('thematic_header','thematic_blogtitle',3);
}
// Call 'remove_thematic_actions' (above) during WP initialization
add_action('init','remove_thematic_actions');

参考サイトでは、親テーマにある「thematic_header」「thematic_blogtitle」をremoveしています。

関数を書き直す場合は、子テーマに「thematic_header」をコピーしてリネームして親テーマ同様にadd_actionします。

function child_thematic_header() {
 ・
 ・
}
add_action('init', 'child_thematic_header');

<追記>

if (!function_exists('thematic_header')) {
  function thematic_header() {
   ・
  }
}

親テーマ側で「function_exists」でチェックしている場合は、子テーマに同じ関数をコピーして書き換えることもできます。

[WordPress] Advanced Custom Fieldsで設定したフィールドに値を動的に入れるフィルター

$
0
0

Advanced Custom Fields」で設定したリンクURLフィールドにPressThisからのURLを入れるのに使いました。

Press This Reloaded」をカスタマイズしていますが、カスタムフィールド「external_link」へURLを入れます。

function load() {
  ・
  add_filter('acf/load_value/name=external_link', array(__CLASS__, 'my_acf_load_value'));
  ・
}
function my_acf_load_value( $value ) {
  $value = self::$url;
  return $value;
}

参考:WordPress › Support » [Plugin: Advanced Custom Fields] Allow PHP in field ‘Default Value’

function my_acf_load_value( $value, $post_id, $field ) {
  if ( $value == 'php_default_hack' ) {
    // do your php magic here...
    $value = 'this is set from php';
  }
  return $value;
}
add_filter( 'acf/load_value/name=my_field', 'my_acf_load_value', 10, 3 );

ACFは設定も簡単だし、フィルターが使えるのがすごいですね。

ContactForm7のプルダウン(Select)でグループ(optgroup)を使う

$
0
0

ContactForm7のプルダウンは

[select menu-469 "タイトル1" "タイトル2" "タイトル3"]

こんな感じで設定しますが、グループにしたい場合もあります。
標準ではグループに対応していません。

参考:Contact Form 7でoptgroupを使う方法 – Daily GLOCALISM

functions.php

<?php
add_action('wp_footer', 'cf7_optlabel');
function cf7_optlabel(){
  global $post;
  $pos = strpos($post->post_content, '[contact-form-7 ');
  if( $pos === false )
    return;
?>
<script>
jQuery(function() {
  //Search for optgroup- items
  var foundin = jQuery('option:contains("optgroup-")');
  jQuery.each(foundin, function(value) {
    //remove optgroup prefix
    var updated = jQuery(this).val().replace("optgroup-","");
    //replace items with optgroup tag
    var replaced = jQuery(this).replaceWith('<optgroup label="'+ updated +'">');
  });
  var foundin2 = jQuery('option:contains("endoptgroup")');
  jQuery.each(foundin2, function(value) {
    //replace items with </optgroup> tag
    var replaced = jQuery(this).replaceWith('</optgroup>');
  });
});
</script>
<?php
}

※ブログに掲載する都合上、7を大文字にしています。

このコードを入れ、フォームの中は

[select menu-469 "optgroup-グループ1" "タイトル1" "タイトル2" "endoptgroup" "optgroup-グループ2" "タイトル3" "endoptgroup"]

にする。
しかし・・・・
optgroupタグで挟まれたキレイなHTMLにはならない。(残念・・・


参考:plugins – WordPress contact form 7 to show the form dropdown menus as like – WordPress Development Stack Exchange

次にこちらの回答を試してみました。
フォームは同じです。

[select menu-469 "optgroup-グループ1" "タイトル1" "タイトル2" "endoptgroup" "optgroup-グループ2" "タイトル3" "endoptgroup"]

functions.php

<?php
add_action('wp_footer', 'cf7_optlabel');
function cf7_optlabel(){
  global $post;
  $pos = strpos($post->post_content, '[contact-form-7 ');
  if( $pos === false )
    return;
?>
<script>
// contact us form - change out optgroup labels
jQuery(function() {
  // search for parent_ items
  var foundin = jQuery('option:contains("optgroup-")');
  jQuery.each(foundin, function(value) {
    var updated = jQuery(this).val().replace("optgroup-","");
    // find all following elements until endoptgroup
    jQuery(this).nextUntil('option:contains("endoptgroup")')
    .wrapAll('<optgroup label="'+ updated +'"></optgroup');
  });
  // remove placeholder options
  jQuery('option:contains("optgroup-")').remove();
  jQuery('option:contains("endoptgroup")').remove();
});
</script>
<?php
}

※ブログに掲載する都合上、7を大文字にしています。

こちらはキレイなHTMLタグになります。

と言うことで今回は下段のコードを使用しました。

[WordPress] 記事一覧などで最初や最後にclassを追加する

$
0
0

記事一覧などで、最初に記事に「class=”first”」、最後の記事に「class=”last”」を追加する

参考:WordPressの記事ループの中で、最初や最後の記事とかを判別する – kanonjiの日記

<?php
//functions.php
/**
 * 記事位置の判定(最初、最後、奇数、偶数)
 * Loop内で<?php if(isLast()) echo 'class="last"'; ?>など
 */
function isFirst(){
	global $wp_query;
	return ($wp_query->current_post === 0);
}
function isLast(){
	global $wp_query;
	return ($wp_query->current_post+1 === $wp_query->post_count);
}
function isOdd(){
	global $wp_query;
	return ((($wp_query->current_post+1) % 2) === 1);
}
function isEvery(){
	global $wp_query;
	return ((($wp_query->current_post+1) % 2) === 0);
}
?>

ライブドアブログからWordPressへ移転する

$
0
0

ライブドアブログからWordpressに移転するメモです。
多くの情報で「ライブドアのbcakup.txtを開いてTAGをTAGSに直す」と書いてありますが、何度やってもタグの設定ができませんでした。

タグ設定できない原因は「WordpressのMovableTypeインポーターにTAGの項目が無い」というもの。
無いものはインポートできないので設定します。

参考:WordPressに MovableTypeのタグを含めてインポート – Qiita

こちらのソースで「tag」を検索すると数カ所見つかると思います。
プラグインの中にある「movabletype-importer.php」を開いて、tagの項目を追加します。
参考ソースとプラグインソースのバージョンが同じなので、丸ごとコピーしても動くかと思います。
自分はtag項目を追加しました。

ライブドアのURLや画像、Wordpressのパーマリンクは、検索で出てくる情報で移転できました。

テーマのCSSキャッシュ

$
0
0

CSSに更新日を付けてキャッシュを回避する

<?php $filename = get_stylesheet_directory().'/style.css'; ?>
<link href="<?php bloginfo('stylesheet_directory'); ?>/style.css?<?php echo date("YmdHis", filectime($filename));?>" rel="stylesheet">

The post テーマのCSSキャッシュ first appeared on WEBPAPRIKA.





Latest Images