Category Archives: PHP code

Link post types

// related posts
// see https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'posts_to_pages',
'from' => 'clients',
'to' => 'portfolio'
) );
}
add_action( 'p2p_init', 'my_connection_types' );

Add custom posts to archive pages

// add custom posts to archive pages
function kweb_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array(
‘post’, ‘clients’, ‘portfolio’
));
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘kweb_add_custom_types’ );

Contact Form 7 add uploaded file to media & attach to custom post

Here:

function my_wpcf7_save($cfdata) {

$formtitle = $cfdata->title;

global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) {
$formData = $form_to_DB->get_posted_data();
$uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
}

if ( $formtitle == 'sync_test') {

// create a new post
$newpost = array(
'post_type' => 'students',
'post_title' => $formData['your-firstname']. ' ' .$formData['your-lastname']. ' - ' .$formData['your-company'],
'post_status' => 'pending');

$newpostid = wp_insert_post($newpost);

// add meta data for the new post
add_post_meta($newpostid, 'rli_meta_students_first_name', $formData['your-firstname']);
add_post_meta($newpostid, 'rli_meta_students_surname', $formData['your-lastname']);
add_post_meta($newpostid, 'rli_meta_students_email', $formData['your-email']);
...
add_post_meta($newpostid, 'rli_meta_students_notes', $formData['your-notes']);
//add_post_meta($newpostid, 'rli_meta_students_photo', $formData['ProfilePhoto']); -- do this after saving image in uploads

$image_name = $formData['ProfilePhoto'];
$image_location = $uploaded_files["ProfilePhoto"];
$image_content = file_get_contents($image_location);
if ($image_content==false){
$check = 'content fail';
} else {
$check = 'content success';
}
$wud = wp_upload_dir();
$upload = wp_upload_bits( $image_name, null, $image_content);
$chemin_final = $upload['url']; // http://www.xxxx.com/wp-content/uploads/2015/09/xxxx.jpg
$filename= $upload['file']; // /home/sites/xxxx.com/public_html/wp-content/uploads/wpcf7_uploads/1565907367/xxxx.jpg
if ($filename>'') {
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $newpostid);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($newpostid, "_thumbnail_id", $attach_id);
add_post_meta($newpostid, 'rli_meta_students_photo', $chemin_final); // $formData['ProfilePhoto']
}
}
}
add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);

added to functions.php

PHP session timeouts

See http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions

/***
* Starts a session with a specific timeout and a specific GC probability.
* @param int $timeout The number of seconds until it should time out.
* @param int $probability The probablity, in int percentage, that the garbage
* collection routine will be triggered right now.
* @param strint $cookie_domain The domain path for the cookie.
*/
function session_start_timeout($timeout=5, $probability=100, $cookie_domain=’/’) {
// Set the max lifetime
ini_set(“session.gc_maxlifetime”, $timeout);

// Set the session cookie to timout
ini_set(“session.cookie_lifetime”, $timeout);

// Change the save path. Sessions stored in teh same path
// all share the same lifetime; the lowest lifetime will be
// used for all. Therefore, for this to work, the session
// must be stored in a directory where only sessions sharing
// it’s lifetime are. Best to just dynamically create on.
$seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), “WIN”) ? “\\” : “/”;
$path = ini_get(“session.save_path”) . $seperator . “session_” . $timeout . “sec”;
if(!file_exists($path)) {
if(!mkdir($path, 600)) {
trigger_error(“Failed to create session save path directory ‘$path’. Check permissions.”, E_USER_ERROR);
}
}
ini_set(“session.save_path”, $path);

// Set the chance to trigger the garbage collection.
ini_set(“session.gc_probability”, $probability);
ini_set(“session.gc_divisor”, 100); // Should always be 100

// Start the session!
session_start();

// Renew the time left until this session times out.
// If you skip this, the session will time out based
// on the time when it was created, rather than when
// it was last used.
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);
}
}