admin管理员组文章数量:1332881
" After completing the form, the following information should be displayed: "Product price [product name], is: [calculated gross amount] PLN gross, tax amount is [calculated tax amount] PLN. ” Data from the form are to be saved to a separate CPT. In addition to the data from the form should also sign up IP and date of completing the form." - thats my task
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
$ip=$_SERVER["REMOTE_ADDR"];
}
function cptpost()
{
//create post object
$userid=get_current_user_id();
$my_post=array(
'post_title' => wp_strip_all_tags ($_POST['name']),
'post_content' => "PRODUCT: " .$_POST['name']. ". PRICE: " .$_POST['price']. " VAT: " .$_POST['vat']. " %." ,
'post_status' => 'publish',
'post_author' => 1,
);
wp_insert_post($my_post);
}
add_action('wp_head', 'cptpost');
I don't know how to add the ip address to the 'post_content' I want in my post something like "PRODUCT: [product], PRICE: [price], VAT: [vat], IP: [ip]"
" After completing the form, the following information should be displayed: "Product price [product name], is: [calculated gross amount] PLN gross, tax amount is [calculated tax amount] PLN. ” Data from the form are to be saved to a separate CPT. In addition to the data from the form should also sign up IP and date of completing the form." - thats my task
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
$ip=$_SERVER["REMOTE_ADDR"];
}
function cptpost()
{
//create post object
$userid=get_current_user_id();
$my_post=array(
'post_title' => wp_strip_all_tags ($_POST['name']),
'post_content' => "PRODUCT: " .$_POST['name']. ". PRICE: " .$_POST['price']. " VAT: " .$_POST['vat']. " %." ,
'post_status' => 'publish',
'post_author' => 1,
);
wp_insert_post($my_post);
}
add_action('wp_head', 'cptpost');
I don't know how to add the ip address to the 'post_content' I want in my post something like "PRODUCT: [product], PRICE: [price], VAT: [vat], IP: [ip]"
Share Improve this question asked Jun 28, 2020 at 12:41 kaahorikaahori 31 bronze badge1 Answer
Reset to default 1I've modified your code and moved $ip inside the function. That way the variable is easily accessible from the function itself.
function cptpost() {
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
$dateTime = date("Y/m/d g:i:sa")
//create post object
$userid = get_current_user_id();
$my_post = array(
'post_title' => wp_strip_all_tags($_POST['name']),
'post_content' => "PRODUCT: " . $_POST['name'] . ". PRICE: " . $_POST['price'] . " VAT: " . $_POST['vat'] . " %. IP:".$ip." Date:".$dateTime,
'post_status' => 'publish',
'post_author' => 1,
);
wp_insert_post($my_post);
}
add_action('wp_head', 'cptpost');
Note: I assume your existing code was working for the post insert part. I just rearranged the code to make sure IP is included.
本文标签: pluginsHow can I add IP address to my post
版权声明:本文标题:plugins - How can I add IP address to my post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312127a2451081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论