我有这个密码。
<?php
add_action(\'add_meta_boxes\', \'ct_meta_add\');
add_action(\'save_post\',\'ct_meta_save\');
add_action(\'save_post\',\'ct_parent\');
// add meta functions
function ct_meta_add() {
add_meta_box(
\'ct_chap\', //id
\'Thông tin chương\', //title
\'ct_meta_show\', //callback function
\'chap\', //post type
\'normal\' //priority
);
}
function get_blogspot_url($string) {
preg_match_all(\'/<a href="(.*?)"/\', $string, $matches);
$string = \'\';
foreach($matches[1] as $val) {
$string .= $val."\\n";
}
$string = str_replace(\'s1600\', \'s0\', $string);
return $string;
}
// save meta functions
function ct_meta_save() {
global $post;
/* Save meta */
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if ( !wp_verify_nonce( $_POST[\'ct_nonce\'], plugin_basename( __FILE__ ) ) ) return;
if( !current_user_can( \'edit_post\' ) ) return;
$meta = array(\'ct_download_link\', \'ct_list_url\', \'ct_list_url2\');
foreach($meta as $dt) {
if($dt == \'ct_list_url2\') {
$_POST[$dt] = get_blogspot_url($_POST[$dt]);
}
if(isset($_POST[$dt]) && $_POST[$dt] !== \'\') {
update_post_meta($post->ID, $dt, $_POST[$dt]);
} else {
delete_post_meta($post->ID, $dt);
}
}
}
?>
<?php
function ct_parent() {
global $post;
if($_POST[\'ct_truyen\'] && $_POST[\'ct_truyen\'] != "") {
//lưu hoặc thay đổi truyện của chap
$ct_post = array();
$ct_post[\'ID\'] = $post->ID;
$ct_post[\'post_parent\'] = $_POST[\'ct_truyen\'];
//thay đổi thời gian cập nhật truyện
$tr_post = array();
$tr_post[\'ID\'] = $_POST[\'ct_truyen\'];
$tr_post[\'post_modified_gmt\'] = current_time(\'mysql\');
$tr_post[\'post_modified\'] = current_time(\'mysql\',7);
remove_action(\'save_post\',\'ct_parent\');
remove_action(\'save_post\',\'tr_parent\');
wp_update_post($ct_post);
wp_update_post($tr_post);
add_action(\'save_post\',\'ct_parent\');
}
}
?>
<?php //save_meta function
function ct_meta_show() { ?>
<?php
global $post;
$ct_download_link = get_post_meta($post->ID, \'ct_download_link\', true) ? get_post_meta($post->ID, \'ct_download_link\', true) : \'\';
$ct_list_url = get_post_meta($post->ID, \'ct_list_url\', true) ? get_post_meta($post->ID, \'ct_list_url\', true) : \'\';
$ct_list_url2 = get_post_meta($post->ID, \'ct_list_url2\', true) ? get_post_meta($post->ID, \'ct_list_url2\', true) : \'\';
?>
<?php wp_nonce_field(plugin_basename(__FILE__), \'ct_nonce\'); ?>
<table class="form-table">
<tbody>
<tr>
<th><label for="ct_download_link">Truyện</label><a class="add-chap-truyen" href="<?php echo admin_url(\'post-new.php?post_type=truyen\'); ?>">thêm truyện</a></th>
<td>
<?php
global $post;
global $wpdb;
$query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = \'truyen\' AND post_status = \'publish\' ORDER BY post_title";
$results = $wpdb->get_results($query, OBJECT);
echo \'<select name="ct_truyen" id="ct_truyen">\';
if(!$_GET[\'truyen_id\'] || !get_the_title($_GET[\'truyen_id\'])) {
echo \'<option value = "">None</option>\';
foreach ($results as $r) {
echo \'<option value="\', $r->ID, \'"\', $r->ID == $post->post_parent ? \' selected="selected"\' : \'\', \'>\', $r->post_title, \'</option>\';
}
} else {
foreach ($results as $r) {
echo \'<option value="\', $r->ID, \'"\', $r->ID == $_GET[\'truyen_id\'] ? \' selected="selected"\' : \'\', \'>\', $r->post_title, \'</option>\';
}
}
echo \'</select>\';
?>
</td>
</tr>
<tr>
<th><label for="ct_download_link">Địa chỉ tải chương</label></th>
<td><input type="text" name="ct_download_link" class="regular-text" value="<?php echo $ct_download_link; ?>"/></td>
</tr>
<tr>
<th><label for="ct_list_url">Địa chỉ ảnh đọc online</label></th>
<td>
<label for="ct_list_url">Địa chỉ ảnh 1</label>
<p><textarea name="ct_list_url" rows="20" class="large-text code"><?php echo $ct_list_url; ?></textarea></p>
<label for="ct_list_url2">Địa chỉ ảnh 2</label>
<p><textarea name="ct_list_url2" rows="20" class="large-text code"><?php echo $ct_list_url2; ?></textarea></p>
</td>
</tr>
</tbody>
</table>
<?php } ?>
<?php
//auto chap title
add_filter( \'default_title\', \'ct_auto_title\');
function ct_auto_title($title) {
if($_GET[\'truyen_id\'] && get_the_title($_GET[\'truyen_id\'])) {
$chap_number = get_number_chap($_GET[\'truyen_id\']);
$title = get_the_title($_GET[\'truyen_id\']) . \' chương \' . ($chap_number+1);
return $title;
} else {
return $title;
}
}
function ct_remove_excess_columns( $columns ) {
unset($columns[\'author\']);
unset($columns[\'comments\']);
unset($columns[\'wpseo-score\']);
unset($columns[\'wpseo-title\']);
unset($columns[\'wpseo-focuskw\']);
unset($columns[\'wpseo-metadesc\']);
return $columns;
}
add_filter( \'manage_edit-chap_columns\' , \'ct_remove_excess_columns\' );
?>
一切正常,但当我保存时,函数
get_blogspot_url($_POST[$dt])
因为$\\u POST[$dt],所以不起作用。
当我更换时get_blogspot_url($_POST[$dt])
具有
get_blogspot_url(\'<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-sfO5YNlgW0Y/UFCaPberRcI/AAAAAAAAANM/1ccm6QRHxLM/s1600/0000.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-sfO5YNlgW0Y/UFCaPberRcI/AAAAAAAAANM/1ccm6QRHxLM/s320/0000.png" width="240" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-a1bQNURFQU0/UFCaRm88fwI/AAAAAAAAANU/uDnHUsvNvS8/s1600/img000005.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-a1bQNURFQU0/UFCaRm88fwI/AAAAAAAAANU/uDnHUsvNvS8/s320/img000005.png" width="199" /></a></div>
<br />\')
这是可行的,所以我认为$\\u POST[$dt]有问题,你能帮我吗?
您可以下载php文件:https://dl.dropbox.com/u/52812919/chap-meta.rar :(很抱歉我的英语不好
SO网友:Adam
据我所见,没有为ct_list_url2
, 执行此代码块时,
$meta = array(\'ct_download_link\', \'ct_list_url\', \'ct_list_url2\');
foreach($meta as $dt) {
if($dt == \'ct_list_url2\') {
$_POST[$dt] = get_blogspot_url($_POST[$dt]);
}
if(isset($_POST[$dt]) && $_POST[$dt] !== \'\') {
update_post_meta($post->ID, $dt, $_POST[$dt]);
} else {
delete_post_meta($post->ID, $dt);
}
...
。。。事实上,这适用于
$meta
大堆您发布的值在哪里都找不到,因此需要将它们传递到函数中。
相反,尝试以下方式,
foreach($_POST as $key => $val) {
$url = get_blogspot_url($val);
if(isset($key) && $val !== \'\') {
update_post_meta($post->ID, $key, $url);
} else {
delete_post_meta($post->ID, $key);
}
但是,您需要小心,因为如果您提交的任何POST变量具有
$key
返回一个空
$val
然后你的
else
条件将运行并删除为给定
$key
. 因此,如果之前在该字段中存储了一个值,则会将其删除。不确定这是否是您的预期目标?
如果你只想ct_list_url2
通过发布变量get_blogspot_url
功能,不包括其他$keys
然后你可以这样做,
foreach($_POST as $key => $val) {
if(isset($key) && $val !== \'\') {
if($key == \'ct_list_url2\') {
$url = get_blogspot_url($val);
update_post_meta($post->ID, $key, $url);
} else {
//uses $val instead of $url to not run through get_blogspot_url() function
update_post_meta($post->ID, $key, $val);
}
} else {
delete_post_meta($post->ID, $key);
}
}
我希望这有帮助!