I am trying to use gform_after_submission to grab a field value(url of a file) then pass the value to a notification so it can be sent as an attachment. This is what I have so far:
add_action("gform_after_submission", "after_submission", 10, 2);
function after_submission($entry, $form){
$pdf = $entry["3"];
return $pdf;
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = 'path/to/file.pdf';
$notification['attachments'] = array( $path );
}
return $notification;
}
Untested but you shouldn't need the first action, only the notification filter.
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$notification['attachments'] = array( $path );
}
return $notification;
}
Thanks Dave, couldn't see your updated code but here's what I managed to do once you told me about converting the URL to a path & it now works perfectly:
function get_file_path_from_url( $file_url ){
return realpath($_SERVER['DOCUMENT_ROOT'] . parse_url( $file_url, PHP_URL_PATH ));
}
add_filter( 'gform_notification_3', 'add_notification_attachments', 10, 3 );
function add_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'Admin Notification' ) {
$path = $entry['3'];
$path = get_file_path_from_url($path);
$notification['attachments'] = array( $path );
}
return $notification;
}
Related
I am using the wp-insert post function to dynamically generate pages from a website I am working on. However, I have been getting these no title posts that increase every time I refresh the page along with the actual pages that are generated from my code.
I have tried solving this by adding a hook into my page creation process, however this does not seem to do anything. I was wondering how I can only create the pages I need without these no title posts appearing.
My code:
if( !class_exists("PageCreator")) {
class PageCreator
{
public function __construct()
{
add_action('init', array($this, 'AddThisPage'));
}
public function AddThisPage()
{
$dirName = "/zotpull/resources/temp/";
$filename = dirname(__DIR__, 2) . $dirName . "useData.txt";
$theFile = fopen($filename, "r");
$msg = fread($theFile, filesize($filename));
fclose($theFile);
$links = explode("\n", $msg);
foreach( array_slice($links, 0, count($links) -1) as $item ) {
$item = str_replace("/","-",$item);
$str2 = substr($item, 5);
$page = array(
'page_template' => 'datePage.php', //Sets the template for the page.
'post_title' => $str2, //The title of your post.
'post_status' => 'publish',
'post_type' => 'page'
);
if ( ! function_exists( 'post_exists' ) ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
$page_exists = post_exists($page['post_title']);
if ($page_exists == 0) {
$insert = wp_insert_post($page);
}
}
}
}
}
Try to use wp_update_post after wp_insert_post. check the below code.
if( !class_exists("PageCreator")) {
class PageCreator{
public function __construct()
{
add_action('init', array($this, 'AddThisPage'));
}
public function AddThisPage()
{
$dirName = "/zotpull/resources/temp/";
$filename = dirname(__DIR__, 2) . $dirName . "useData.txt";
$theFile = fopen($filename, "r");
$msg = fread($theFile, filesize($filename));
fclose($theFile);
$links = explode("\n", $msg);
foreach( array_slice($links, 0, count($links) -1) as $item ) {
$item = str_replace("/","-",$item);
$str2 = substr($item, 5);
$page = array(
'page_template' => 'datePage.php', //Sets the template for the page.
'post_title' => $str2, //The title of your post.
'post_status' => 'publish',
'post_type' => 'page'
);
if ( ! function_exists( 'post_exists' ) ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
$page_exists = post_exists($page['post_title']);
if ($page_exists == 0) {
$insert = wp_insert_post($page);
$my_post = array(
'ID' => $insert,
'post_title' => $str2
);
wp_update_post( $my_post );
}
}
}
}
}
I have a wordpress function that adds a query string 'nocfcache=1' to a single page.
function nocfcache_query_string( $url, $id ) {
if( 42 == $id ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Issue: How to use multiple page ids in the function so as to make sure they all have the query string appended.
What I have tried so far:
function nocfcache_query_string( $url, $id ) {
$id = array (399, 523, 400, 634, 636, 638);
if(in_array($post->ID, $id)) {
$url = add_query_arg( 'nocfcache', true, get_permalink( $post->ID ));
return $url;
exit;
}
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Add a custom checkbox field/option for page/post/any (you can use ACF todo it easily).
You can use it to check if page requires to add the query string or not.
function nocfcache_query_string( $url, $id ) {
$custom_checkbox = get_post_meta($id, 'custom_field_name', true); // though for ACF you also use get_field('custom_field_name', $id);
if( $custom_checkbox ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
I'm trying to get the $pi1 and $pi2 variable values inside of the add_custom_price function, but nothing seems to be working.
I have looked at setting variables to be accessible from function classes but I'm not sure I understand how to access them correctly.
add_filter( 'gform_confirmation', array(gravity_pi,custom_confirmation), 10, 4 );
class gravity_pi {
public $pi1;
public $pi2;
public function custom_confirmation( $confirmation, $form, $entry, $ajax, $product_id, $pi1, $pi2 ) {
if( $form['id'] == '2' ) {
$post = get_post( $entry['post_id'] );
$this->pi1 = rgar( $entry, '20' );
$this->pi2 = rgar( $entry, '21' );
$exclude_list = array("pi24","pi64","pi65","pi66","pi67","pi68","pi69","pi70","pi71","pi72","pi73","pi74","pi75","pi76","pi77","pi78","pi79","pi80","pi81","pi82");
if(!in_array($this->pi1, $exclude_list) && !empty($this->pi1)){
$target_product_id = '86';
$pid1 = '86';
}else{
$pid1 = preg_replace("/[^0-9,.]/", "", $this->pi1 );
}
if(!in_array($pi2, $exclude_list) && !empty($this->pi2)){
$target_product_id = '87';
$pid2 = '87';
}else{
$pid2 = preg_replace("/[^0-9,.]/", "", $this->pi2);
}
$product_ids = ''.$pid1.','.$pid2.'';
$url = 'https://*****.com/cart/?add-to-cart='.$product_ids.'';
$confirmation = array( 'redirect' => $url );
}
return $confirmation;
}
public function add_custom_price( $cart_object, $entry,$form, $field, $input_id ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
if( 86 == $value['data']->id ) {
$value['data']->set_price( $this->pi1 );
}
if( 87 == $value['data']->id ) {
$value['data']->set_price( $this->pi2 );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', array(gravity_pi,add_custom_price));
You need to use $this to get/set any property in the class.
// set pi1 property value
$this->pi1 = 10;
//print/get property value
echo $this->pi1;
When you're setting the class properties (pi1 & pi2) you need to reference them in the same way you access them. Ex.
<?php
...
$this->pi1 = rgar($entry, '20');
Change all references (except where you declare them) of $pid1 to $this->pid1. Do this for $pid2 too.
Check this video out - https://www.youtube.com/watch?v=4c4nP7GLL1c
I have been able to add a sortable column to my Wordpress userpage that displays the users last know IP address.
The problem I seem to be having is getting the column to sort by the IP's numbers and not the usernames alphabetically.
I feel im missing something simple and could use another set of eyes.
<?php
class Register_IP_Multisite {
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
add_action( 'user_register', array( $this,'log_ip') );
add_action( 'edit_user_profile', array( $this,'edit_user_profile') );
add_action( 'manage_users_custom_column', array( $this,'columns'), 10, 3);
add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns_wsp') );
add_filter( 'request', array( $this ,'users_orderby_column_wsp') );
add_filter( 'plugin_row_meta', array( $this ,'donate_link'), 10, 2 );
if ( is_multisite() ) {
add_filter('wpmu_users_columns', array( $this ,'column_header_signup_ip'));
} else {
add_filter('manage_users_columns', array( $this ,'column_header_signup_ip'));
}
}
public function log_ip($user_id){
$ip = $_SERVER['REMOTE_ADDR']; //Get the IP of the person registering
update_user_meta($user_id, 'signup_ip', $ip); //Add user metadata to the usermeta table
}
public function edit_user_profile() {
$user_id = (int) $_GET['user_id'];
?>
<h3><?php _e('Signup IP Address', 'register-ip-mutisite'); ?></h3>
<p style="text-indent:15px;"><?php
$ip_address = get_user_meta($user_id, 'signup_ip', true);
echo $ip_address;
?></p>
<?php
}
public function column_header_signup_ip($column_headers) {
$column_headers['signup_ip'] = __('IP Address', 'register-ip-multisite');
return $column_headers;
}
public function users_sortable_columns_wsp($column_headers) {
$customwsp = array(
// meta column id => sortby value used in query
'signup_ip' => 'user_ip_address',
);
return wp_parse_args($customwsp, $column_headers);
}
public function users_orderby_column_wsp( $varswsp ) {
if ( isset( $varswsp['orderby'] ) && 'signup_ip' == $varswsp['orderby'] ) {
$varswsp = array_merge( $varswsp, array(
'meta_key' => 'signup_ip',
'orderby' => 'meta_value'
) );
}
return $varswsp;
}
public function columns($value, $column_name, $user_id) {
global $modewsp;
$modewsp = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
if ( $column_name == 'signup_ip' ) {
$ip = get_user_meta($user_id, 'signup_ip', true);
if ($ip != ""){
$theip = $ip;
if ( has_filter('ripm_show_ip') ) {
$theip = apply_filters('ripm_show_ip', $theip);
}
return $theip;
} else {
$theip = '<em>'.__('None Recorded', 'register-ip-multisite').'</em>';
return $theip;
}
}
$user_ip_address = strtotime(get_date_from_gmt($user->signup_ip));
return $value;
}
}
new Register_IP_Multisite();
I want to change text of published under date column of post list in wordpress.
see below image
You can try using following way.
<?php
function my_custom_columns( $columns ) {
unset( $columns['date'] );
$columns['mydate'] = 'My Custom Date';
return $columns;
}
function my_format_column( $column_name , $post_id ) {
if($column_name == 'mydate'){
echo get_the_time( 'l, F j, Y', $post_id )."<br>".get_post_status( $post_id );
}
}
function my_column_register_sortable( $columns ) {
$columns['mydate'] = 'mydate';
return $columns;
}
function my_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'mydate' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'orderby' => 'date'
) );
}
return $vars;
}
function my_column_init() {
add_filter( 'manage_posts_columns' , 'my_custom_columns' );
add_action( 'manage_posts_custom_column' , 'my_format_column' , 10 , 2 );
add_filter( 'manage_edit-post_sortable_columns', 'my_column_register_sortable' );
add_filter( 'request', 'my_column_orderby' );
}
add_action( 'admin_init' , 'my_column_init' );
?>