我已经查看了源代码,不幸的是,我没有找到一种不保存信息就传递信息的好方法。这太糟糕了,时间太长了,因为这真的不是什么需要挽救的事情。
解决方法是启用PHP Sessions 将以下内容放在functions.php
:
if (!session_id()) {
session_start();
}
现在您可以使用
$_SESSION
变量,如下所示:
$_SESSION[ \'your-key\' ] = \'your-value\';
按如下方式创建表单字段:
function wpse_154330_attachment_fields_to_edit( $form_fields, $post ) {
$current_screen = get_current_screen();
// we are not saving, so no need to show the field on the attachment page
if ( $current_screen->id == \'attachment\' ) {
return $form_fields;
}
$form_fields[\'fancyboxGroup\'] = array(
\'label\' => \'fancybox group\',
\'input\' => \'text\',
\'value\' => \'\', // leave the value empty
\'helps\' => \'use this to group images in fancybox\',
);
return $form_fields;
}
add_filter(
\'attachment_fields_to_edit\',
\'wpse_154330_attachment_fields_to_edit\',
10,
2
);
使用如下会话变量:
function wpse154330_save_attachment_field( $post, $attachment ) {
// we\'re only setting up the variable, not changing anything else
if ( isset( $attachment[ \'fancyboxGroup\' ] ) {
$_SESSION[ \'fancyboxGroup\' ] = $attachment[ \'fancyboxGroup\' ];
}
return $post;
}
add_filter(
\'attachment_fields_to_save\',
\'wpse154330_save_attachment_field\',
10,
2
);
相应地修改输出:
function wpse154330_image_send_to_editor(
$html,
$id,
$caption,
$title,
$align,
$url,
$size,
$alt = \'\'
) {
// no need to modify the output, if no fancybox group is given
if (
empty( $_SESSION[ \'fancyboxGroup\' ] )
|| ! isset( $_SESSION[ \'fancyboxGroup\' ] )
) {
return $html;
}
$classes = \'fancybox\';
if ( preg_match( \'/<a.*? class=".*?">/\', $html ) ) {
$html = preg_replace(
\'/(<a.*? class=".*?)(".*?>)/\',
\'$1 \' . $classes . \'$2\',
$html
);
} else {
$html = preg_replace(
\'/(<a.*?)>/\',
\'$1 class="\'
. $classes
. \'" data-fancybox-group="\'
. $_SESSION[ \'fancyboxGroup\' ]
. \'" >\',
$html
);
}
unset( $_SESSION[ \'fancyboxGroup\' ] );
return $html;
}
add_filter(
\'image_send_to_editor\',
\'wpse154330_image_send_to_editor\',
10,
8
);
就是这样。应该很自我解释,否则就问吧。