Ребята, need help. Допиливаю плагин Image Autorename, wordpress, он сделан чтобы менять alt, title и description загруженной картинки. Замена этих параметров происходит по Хуку save_post, но вордпресс до этого успевает прописать путь к картинке по старому title. Нужен какой-то другой хук. Add_atachment почему-то не срабатывает.
<?php
/*
* Plugin Name: Image Autorename
* Plugin URI:
http://pixelix.ru/image-autorename/* Description: Automatically complete fields for files attached to the posts.
* Version: 2.0.3
* Author: Pixelix
* Author URI:
http://pixelix.ru* License: GPL2
* Text Domain: image-autorename
* Domain Path: /lang/
*/
function iar_init() {
load_plugin_textdomain( 'image-autorename', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
add_action('init', 'iar_init');
function image_autorename($post_id) {
$the_title = get_the_title($post_id);
$get_att = array( 'post_type' => 'attachment', 'post_parent' => $post_id );
$attachments = get_posts( $get_att );
foreach ( $attachments as $attachment ) {
$iar_status = get_post_meta( $attachment->ID, 'iar_status', true );
if ($iar_status != 'no'){
$iar_update = array();
$iar_update['ID'] = $attachment->ID;
$iar_update['post_content'] = $the_title;
$iar_update['post_title'] = $the_title;
//*$iar_update['post_excerpt'] = $the_title;
wp_update_post( $iar_update );
update_metadata('post', $attachment->ID, '_wp_attachment_image_alt', $the_title);
}
}
}
add_action( 'save_post', 'image_autorename' );
function add_iar_column( $columns ) {
$columns[ 'iar_status' ] = __('Autorename', 'image-autorename');
return $columns;
}
add_filter( 'manage_upload_columns', 'add_iar_column', 10, 2 );
function fill_iar_column( $column_name, $post_id ) {
switch( $column_name ) {
case 'iar_status':
$iar_status = get_post_meta( $post_id, 'iar_status', true );
if ($iar_status == 'no'){
_e('Off', 'image-autorename');
echo ' — ';
echo '<a href="admin.php?action=switch_iar_status&post=' . $post_id . '">';
_e('Turn on', 'image-autorename');
echo '</a>';
}
else{
_e('On', 'image-autorename');
echo ' — ';
echo '<a href="admin.php?action=switch_iar_status&post=' . $post_id . '">';
_e('Turn off', 'image-autorename');
echo '</a>';
}
break;
}
}
add_action( 'manage_media_custom_column', 'fill_iar_column', 10, 2 );
function add_iar_status_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'iar_status', true );
if($field_value == 'no'){
$select = 'selected="selected"';
}
$form_fields['iar_status'] = array(
'label' => __('Autorename', 'image-autorename'),
'helps' => __('Are fields will be complete after parent post will be update?', 'image-autorename'),
'input' => 'html',
'html' => '
<select name="iar_status_field" id="iar_status_field">
<option value="yes">'.__('On', 'image-autorename').'</option>
<option value="no" '.$select.'>'.__('Off', 'image-autorename').'</option>
</select>
',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_iar_status_field', 10, 2 );
function save_iar_status_field( $attachment_id ) {
if ( isset( $_REQUEST['iar_status_field'] ) ) {
$iar_status = $_REQUEST['iar_status_field'];
if ($iar_status == 'no'){
update_post_meta( $attachment_id, 'iar_status', 'no' );
}
else {
update_post_meta( $attachment_id, 'iar_status', 'yes' );
}
}
}
add_action( 'edit_attachment', 'save_iar_status_field' );
function switch_iar_status(){
$post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
$iar_status = get_post_meta( $post_id, 'iar_status', true );
if ($iar_status == 'no'){
update_post_meta( $post_id, 'iar_status', 'yes' );
}
else{
update_post_meta( $post_id, 'iar_status', 'no' );
}
wp_redirect( admin_url( 'upload.php') );
}
add_action( 'admin_action_switch_iar_status', 'switch_iar_status' );
?>