カスタムフィールドをサイト内検索の対象にできる、WordPressプラグイン「Search Everything」を使っているサイトで不具合があったので、修正方法をメモ。
エラーの内容
記事の新規投稿画面で必要事項を入力して「公開」ボタンを押すと以下のエラーが画面に出力される。
1 |
Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /www/public_html/wp-content/plugins/search-everything/search-everything.php:927 Stack trace: #0 /www/public_html/wp-includes/class-wp-hook.php(288): se_post_publish_ping(1181) #1 /www/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /www/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /www/public_html/wp-includes/post.php(4161): do_action('publish_post', 1181, Object(WP_Post)) #4 /www/public_html/wp-includes/post.php(3588): wp_transition_post_status('publish', 'draft', Object(WP_Post)) #5 /www/public_html/wp-includes/post.php(3746): wp_insert_post(Array, false) #6 /www/public_html/wp-admin/includes/post.php(377): wp_update_post(Array) #7 /www/public_html/wp-a in /www/public_html/wp-content/plugins/search-everything/search-everything.php on line 927 |
(サーバー固有のパスが入っているので、一部加工してます)
記事は公開されずに下書きとなり、入力したカスタムフィールドの内容は消えているが、その下書きを再編集して公開すると無事公開できるという内容。
ただ、カスタムフィールド以外のタイトルと本文は消えていないので、カスタムフィールドを使っていない投稿タイプは影響を受けないと思われる。
エラーを修正する方法
「Search Everything」のプラグインファイルを直接修正します。
プラグインファイルを直接修正するということはあまりやりたくない方法ですが、1年以上も更新が滞っているので今回はこの方法をとります。
wp-content/plugins/search-everything/search-everything.php
の913行目以降の全コードを以下のコードに書き換える。
※コード書き換え前には、ファイルのコピーをとっておくことをお勧めします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function se_post_publish_ping($post_id) { //should happen only on first publish $status = false; if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { $permalink = get_permalink($post_id); $zemanta_response = se_api(array( 'method' => 'zemanta.post_published_ping', 'current_url' => $permalink, 'post_url' => $permalink, 'post_rid' => '', 'interface' => 'wordpress-se', 'deployment' => 'search-everything', 'format' => 'json' )); if (!is_wp_error($zemanta_response)) { $status = json_decode($zemanta_response['body'])->status; } } return $status; } add_action( 'publish_post', 'se_post_publish_ping' ); |
↓これは書き換え前のエラーのあるコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function se_post_publish_ping($post_id) { //should happen only on first publish $status = false; if( !empty( $_POST['post_status'] ) && ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { $permalink = get_permalink($post_id); $zemanta_response = se_api(array( 'method' => 'zemanta.post_published_ping', 'current_url' => $permalink, 'post_url' => $permalink, 'post_rid' => '', 'interface' => 'wordpress-se', 'deployment' => 'search-everything', 'format' => 'json' )); $response = json_decode($zemanta_response['body']); if (isset($response->status) && !is_wp_error($zemanta_response)) { $status = $response->status; } } return $status; } add_action( 'publish_post', 'se_post_publish_ping' ); |
↓これは修正差分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ diff search-everything.php.org search-everything.php 916c916 < if( !empty( $_POST['post_status'] ) && ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { --- > if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { 927,929c927,928 < $response = json_decode($zemanta_response['body']); < if (isset($response->status) && !is_wp_error($zemanta_response)) { < $status = $response->status; --- > if (!is_wp_error($zemanta_response)) { > $status = json_decode($zemanta_response['body'])->status; 934d932 < 936d933 < |