Add multiple custom fields to post/product archive - advanced-custom-fields

Hello I'm not really into PHP. I want to add multiple advanced custom fields to post/product archive in Wordpress. Here is the code I have that works but it only adds one field. Any help will be greatly appreciated.
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field value.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta;
return $custom_excerpt;
}

To add multiple advanced custom fields to the post/product archive in WordPress, you will need to use the same get_field function to grab the values of each field, and then concatenate them to the excerpt in the same way as you are doing with the current field. Here's an example of how you can add two more fields to the code you provided:
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field values.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
$custom_meta_2 = get_field( 'my_field_2', $post->ID );
$custom_meta_3 = get_field( 'my_field_3', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta . $custom_meta_2 . $custom_meta_3;
return $custom_excerpt;
}
Make sure to replace the field names ('my_field_2', 'my_field_3') with the actual field names of the custom fields you want to add.
Also, you can use a loop to add multiple fields dynamically.
$custom_meta = "";
if( function_exists('get_field') && isset($post->ID) ) {
for($i=1;$i<=3;$i++)
{
$custom_meta .= get_field( 'my_field_'.$i, $post->ID );
}
}

Related

MediaWiki - Hook to edit/alter page title on creation

I'm looking to (programmatically) edit/alter the title of a page when its created and before its saved. I've tried a couple hooks to no avail. I can access the title being saved along with other info which ill be used to alter the title, but can not find the right call to save the altered title.
It is for a private/local lan wiki (currently MediaWiki version 1.38.1 ).
When a new article is created with in a certon category I want to number it with a prefix of #### - based on the number of articles already in the category. At the top of the category page itself I have a <inputbox> which pulls a template that has the wiki syntax for the category in it [[Category:BlaBla]]. When the article is saved I'm doing a check to make sure its a new article and some ofther checks for the info I need, which is working fine, but I can not save the new altered page name.
I've tried the following hooks to no avail.
onMultiContentSave
onArticlePrepareTextForEdit
https://www.mediawiki.org/wiki/Manual:Hooks/MultiContentSave
https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePrepareTextForEdit
Heres acouple snippets ive been testing with, both do as i want, aside from saving the altered page name.
public static function onArticlePrepareTextForEdit( WikiPage $wikiPage, ParserOptions $parserOptions ) {
return;
$exists = $wikiPage->exists();
if ($exists == 1) {
#return true;
}
$getTitle = $wikiPage->getTitle();
# check if title starts with 0000, exit if so, no work needs to be done
if (self::titleCheck($getTitle)) {
#return true;
}
$checkCategories = $wikiPage->getCategories();
$inMalak = false;
foreach ($checkCategories as $value) {
if ($value == "Category:Malak") {
$inMalak = true;
}
}
if ($inMalak == 1) {
$newTitle = self::newTitlePre() . $getTitle;
#$wikiPage->setTitle($newTitle);
print(">" . $newTitle . "<br>");
}
self::pr($newTitle);
}
public static function onMultiContentSave(RenderedRevision $renderedRevision, UserIdentity $user, CommentStoreComment $summary, $flags, Status $hookStatus){
#return;
$revision = $renderedRevision->getRevision();
$getTitle = $revision->getPageAsLinkTarget();
if (self::titleCheck($getTitle)) {
return true;
}
#$titleOBJ = $revision->Title();
$title = $revision->getId();
$parent_id = $revision->getId();
$content = $revision->getContent( SlotRecord::MAIN );
$new_content = $content->getText();
#$test = $revision->ParserOutput();
$parent_id = "";
if ($parent_id == "") {
$pos = strpos($new_content, "[[Category:Malak]]");
if ($pos) {
$newTitle = self::newTitlePre() . $getTitle;
#$wikiPage->setTitle($newTitle);
}
}
self::pr($newTitle);
}
EDIT........
Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.
Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.
public static function onEditFormPreloadText(string &$text, Title &$title ) {
global $wgOut;
if ( isset( $_GET["MalakHere"] ) ) {
return true;
}
$pos = strpos($text, "[[Category:Malak]]");
if ($pos) {
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$urlTitle = urlencode($_GET["title"]);
$newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
$newTitle = self::newTitlePre() . $title->prefixedText;
$url = $newURL . "&title=" . $newTitle;
return $wgOut->redirect($url);
}
return true;
}
Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.
Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.
public static function onEditFormPreloadText(string &$text, Title &$title ) {
global $wgOut;
if ( isset( $_GET["MalakHere"] ) ) {
return true;
}
$pos = strpos($text, "[[Category:Malak]]");
if ($pos) {
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$urlTitle = urlencode($_GET["title"]);
$newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
$newTitle = self::newTitlePre() . $title->prefixedText;
$url = $newURL . "&title=" . $newTitle;
return $wgOut->redirect($url);
}
return true;
}

Is it possible to use 'return' instead of 'echo' to output a wpdb query?

I have this working function meant to be used for a WordPress Multisite Database query. It's perfectly okay but I want to know if it is possible to output the desired results using return instead of echo.
Every solution and example I've come across uses echo.
function akwQueries_byMembership( $memID ){
switch_to_blog( akw_memSiteID() );
global $wpdb;
$user_id = get_current_user_id();
$prefix = $wpdb->prefix;
$membershipTable = $prefix.'membership_users_table';
$query="SELECT members_id, status
FROM $membershipTable
WHERE members_id=$memID AND user_id=$user_id AND status='active'";
$req_memid=$wpdb->get_row($query);
if( $req_memid !== null ){
// My question arises from here...
echo 'Good to go';
} else {
echo 'Not ok';
}
restore_current_blog();
}
I tried using return and it's so lifeless. I would love, for instance, to return true.
Move the restore function up above the return statement and then return the boolean.
function akwQueries_byMembership( $memID ){
switch_to_blog( akw_memSiteID() );
global $wpdb;
$user_id = get_current_user_id();
$prefix = $wpdb->prefix;
$membershipTable = $prefix.'membership_users_table';
$query="SELECT members_id, status
FROM $membershipTable
WHERE members_id=$memID AND user_id=$user_id AND status='active'";
$req_memid=$wpdb->get_row( $query );
restore_current_blog();
return $req_memid !== null;
}

Use the loop to check if DB record exists in WordPress?

I have a somewhat unorthodox (i guess) question. I have a function that adds a record to my MYSQL DB, with location lat and lng info. However sometimes that function is not working correctly, causing for the the DB record not to be created. Therefore I would like to create a loop to keep checking if the function created the DB record and if not, do the function again.
Just to be clear; the function I use is a default GEOMyWP function (reference). Sometimes it is not working properly, I guess due to the Google maps API response time.
I have the following in my functions.php and it seems to be working. I really would like to know if this is a proper way to accomplish the above, since I am not sure. Thanks
global $wpdb;
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
if ($myccount <= 0) :
while ($myccount <= 0) :
gmw_pt_update_location( $args ); //The function I would like to check.
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
endwhile;
endif;
Below is the function that i use to import the address, when creating a post for a CPT
function gmw_update_post_type_post_location( $post_id ) {
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
// check autosave //
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
//check if user can edit post
if ( !current_user_can( 'edit_post', $post_id ) )
return;
//Add post Meta
add_post_meta($post_id, 'address', $_POST['address'], false);
//get the address from the custom field "address"
$addressarray = get_post_meta( $post_id, 'address', true );
$address = $addressarray[0];
//$address = $_POST['address'];
//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;
//include the update location file file
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
//make sure the file included and the function exists
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
//Create the array that will pass to the function
$args = array(
'post_id' => $post_id, //Post Id of the post
'address' => $address // the address we pull from the custom field above
);
//Start counting 1
global $wpdb;
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
if ($myccount <= 0) :
while ($myccount <= 0) :
//Add location
gmw_pt_update_location( $args );
//Start counting 2
$myccount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."gmw_locations WHERE object_id = %d", $post_id));
endwhile;
endif;
}
//execute the function whenever post type is being updated
add_action( 'save_post_mpp-gallery', 'gmw_update_post_type_post_location', 13, 1 );
Function that is inserting a record to the db is eventually: gmw_update_location
This function can be found in the GEOMyWP plugin. See this link for some reference: https://github.com/Fitoussi/geo-my-wp/blob/master/includes/gmw-location-functions.php#LC194

How to add auto class to wordpress upload media

How to add class to new wordpress upload item in post?
how to add from automatic:
upload file nameV
class="fileuploadclass"
?
Just paste following code in your current theme function.php file.
if ( ! function_exists( 'auto_custom_class_add_for_media_attchment' ) ) :
function auto_custom_class_add_for_media_attchment( $html, $id ) {
$attachment = get_post( $id );
$mime_type = $attachment->post_mime_type;
// Here i added if condition only for pdf mine type, you can use whatever mime_type you require or remove condition for all.
if ( $mime_type == 'application/pdf' ) {
$src = wp_get_attachment_url( $id );
$html = '<a class="fileuploadclass" href="'. $src .'">'. $attachment->post_title .'</a>';
}
return $html;
}
endif;
add_filter('media_send_to_editor', 'auto_custom_class_add_for_media_attchment', 20, 3);

codeigniter - convert html to pdf

I have a little problem. I have html page and I want to convert to pdf. My index page has a list that will get to the database and click on "Download PDF", I put this list in a PDF file.
My controller:
<?php
class pdf_c extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper(array('url', 'mediatutorialpdf'));
}
function index($download_pdf = ''){
$ret = '';
$ID = 1;
$pdf_filename = 'user_info_'.$ID.'.pdf';
$link_download = ($download_pdf == TRUE)?'':anchor(base_url().'index.php/true', 'Download PDF');
$query = $this->db->query("SELECT * FROM `ci_pdf_user` WHERE `id` = '{$ID}' LIMIT 1");
if($query->num_rows() > 0)
{
$user_info = $query->row_array();
}
$data_header = array(
'title' => 'Convert codeigniter to pdf'
);
$data_userinfo = array(
'user_info' => $user_info,
'link_download' => $link_download
);
$header = $this->load->view('header',$data_header, true);
$user_info = $this->load->view('user_table', $data_userinfo, true);
$footer = $this->load->view('footer','', true);
$output = $header.$user_info.$footer;
if($download_pdf == TRUE)
{
generate_pdf($output, $pdf_filename);
}
else
{
echo $output;
}
}
}
?>
The problem is when I click the button "Download PDF" should redirect me to the function index () and get the $ download_pdf = true. And so called generate_pdf function () that will generate the PDF.
I think the problem is in the variable $ link_download, but can not solve the problem.
Thanks
I think that you could try with:
function index(pdf = 0)...
Then check that optional parameter with:
$pdf = $this->uri->segment(2, 0); //not sure, should be 2? try it...`
And then, if $pdf=='1' (send nummber rather than string 'true') ...etc,etc...