Im using Drupal 6.x. In my page i have the following code which prints a paged table.
$headers = array(array('data' => t('Node ID'),'field' => 'nid','sort'=>'asc' ),
array('data' => t('Title'),'field' => 'title'),
);
print theme('pager_table','SELECT nid,title FROM {node_revisions}', 5, $headers );
Is there a way i can pass the rows of a table as an array to the theme function ?
I don't know theme_pager_table, it's not a part of Drupal core. What you can do, it to wrap your sql in pager_query(), then you can loop through your results and create the table rows like normal. pager_query() will handle adding the LIMIT and OFFSET in the query.
Doing this you cam use the normal theme_table and just add the pager with theme_pager. (Remember to use the theme, wrapper function instead of calling the theme functions directly)
Related
I need to add variations to my Woocommerce products programmatically and I borrowed the code from this answer thread:
Create programmatically a WooCommerce product variation with new attribute values
It works, but gives me two variations when I pass this data array:
$variation_data = array(
'attributes' => array(
'kidssize' => '2'
),
'sku' => '',
'regular_price' => '120',
'sale_price' => '',
'stock_qty' => '',
);
My guess is that funcntion fires two times.
Since i am a noob in php and backend in general all I know how to call a function is from some template file and visiting the page.
And to prevent other visitors from triggering it I use
if(isset($_GET['**parameter Only I know**']))
… wrap and call it going to the page with set parameter;
I understand that this is a really bad way for doing that, but how do I do it otherwise if I need to call function once and never use it again?
And is even firing twice a problem here or is it something wrong with my array?
EDIT:
Here's a detailed process of what i do:
i put the function from the link above in functions.php,
then put the call in footer.php of my theme with above mentioned wrap (the parameter is irrelevant - it could be anything, because it's just used as a trigger)
and go to the page with said parameter to trigger the call,
load the page only once and look for the result in admin panel.
And it has 2 variations always, even if i add more attributes to an array it will return 2 variatons of the last attributes array item;
I have a need to change a plugin that creates/edits entries to the wp_posts table. Currently, it is lacking in that it cannot target some specific fields. I am endeavoring to edit the plugin so I can change these fields as well with the plugin. Specifics below.
How would I generally target this table then create/edit entries?
I have in mind something like this, which was copied from here.:
$my_post = array(
'post_title' => '#Enter the title#',
'post_content' => '#Enter the Content#',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
I assume this is possible because the plugin I'm hoping to change already makes changes to the wp_posts table. How do I do this? I have programming experience in C#, but my PHP and WP knowledge is severely lacking. I assume there's a few WordPress key functions that I'm looking for, but I simply don't know what they are or how to find them (I might be searching for the wrong terms). wp_insert_post() looks very promising, but the documentation seems to imply that it won't work for my specific needs.
Specifics
I'm using a great plugin called Really Simple CSV importer (RSC), which allows you to create/update posts with a csv. It seems to be able to target nearly every field (e.g. post_title, post_id, post_parent, post_content, _wp_page_template, etc.). This is quite a boon if you have to create hundreds of pages regularly (which I do).
The problem is that it won't target two specific fields, which are titled group_access and tag_list. I put those fields as columns in my csv, but they are not added to the mySQL database, and thus are not changed on the site. I believe these fields are specific to a plugin called iMember360, which is a plugin that ties into an InfusionSoft account.
Naturally, I've tried contacting RSC support, but have received no replies at all. I've spoken to iMember360 support at length, and the best they can give me without doing the work themselves is that they use the action hook import_end to make changes to the table, so if RSC is not using it, then it won't affect those fields. They also say that iMember360 has an import/export function, but it is only for the plugin's specific features, and ties in with the WordPress XML import/export feature. Clearly, this RSC plugin does not do that.
Regardless of these limitations, it seems to me like if the field exists in the table, then you should be able to edit it, so I tend to think that the RSC plugin is simply lacking in this functionality, probably only targeting WP default fields only.
What I've tried:
I have tried editing the plugin PHP directly, with the assumption that the plugin simply does not have an entry for the group_access and tag_list fields.
One of the PHP files contained:
// (string, comma separated) name of post tags
$post_tags = $h->get_data($this,$data,'post_tags');
if ($post_tags) {
$post['post_tags'] = $post_tags;
}
I simply copied and pasted it right above it and changed post_tags to group_access. It didn't work.
I also found, and added to:
$attachment = array(
'post_mime_type' => $mime_type ,
'post_parent' => $this->postid ,
'post_author' => $this->post->post_author ,
'post_title' => $title ,
'post_content' => $content ,
'post_excerpt' => $excerpt ,
'post_status' => 'inherit',
'menu_order' => $this->media_count + 1,
////////// added
'group_access' => $group_access ,
'tag_list' => $tag_list ,
//////
);
That didn't do anything either.
There are two ways to edit the wp_posts table that I know of. The first is with the wp_update_post() and wp_insert_post() functions. These are typical WP functions that are used like any others. You just have to pass the correct parameters. For example:
$my_post = array(
'ID' => $updatePostID, // User defined variable; the post id you want to update.
'post_title' => 'Update Title',
'post_content' => 'Update Content',
);
// Update the post into the database
$post_id = wp_update_post( $my_post, true ); // Returns 0 if no error; returns post id if there was an error.
// Check if table was updated without error. For debugging. Remove from your code if it all works, before publishing.
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}
Simply pass an array with the predefined field names to the functions and they will then run correctly. The update function is for updates, naturally, and the insert function creates new table entries (rows). There is a limitation, however. These two functions will only operate on the WP predefined fields. So, if your table contains some custom fields, then you will have to use the $wpdb class.
The other way to update your mySQL tables from WordPress is to use the $wpdb class directly. This class is far more expansive than the wp_update_post() and wp_insert_post() functions. The first difference is that it can edit many other mySQL tables, not just the wp_posts table. It can also edit non-WP fields. For example:
global $wpdb; // This calls the use of the class. I actually do not think this is necessary, but I do it anyway.
$dbresult = $wpdb->update($wpdb->posts, // Returns 0 if no errors. Post ID if errors occurred.
// Notice posts instead of wp-posts. Prefixes are not necessary with this class.
['post_title' => 'Update Title',
'post_content' => 'Update Content',
'custom_field' => $customField, ], // User defined table column and variable.
['ID' => $updatePostID, ]); // User defined variable; the post id.
if (false === $dbresult) {
echo 'An error occurred while updating...';
$errors = $post_id->get_error_messages();
}
I'm not very familiar with either of these options, but there seems to a massive difference in the width of functionality, so there may be considerations you should take before using one over the other. I've asked a question to that end: https://wordpress.stackexchange.com/q/259043/38365 One reply says:
Downsides of $wpdb are that you do need to know SQL and you need to be conscious of normal data sanitization and when to use prepare() and what functions already prepare your statement which are hardly downsides at all. The $wpdb Class is by nature more powerful than any of the default WordPress functions because you have access to all the data, custom or not, assuming you know the proper SQL to get, modify, or remove it.
TL;DR $wpdb is more powerful but less friendly and forgiving.
That makes sense to me. Basically, don't fiddle with $wpdb unless you know what you are doing or absolutely have to.
I'm by no means a PHP guru and have been given the task of updating a WordPress site. I've managed to get my php-includer plugin to work so that I can use a shortcode within the content area which the editor can then reference a stand-alone file which will then get injected into the main page.
The code I'm using is this:
<?php
function PHP_Include($params - array()) {
extract(shortcode_atts(array(
'file' => 'default'
), $params));
ob_start();
include(get_stylesheet_directory() . "/inc/$file.php");
return ob_get_clean();
}
add_shortcode('phpinclude', 'PHP_Include');
?>
I've saved this as a plugin and then using [phpinclude file='testfile'] in the content on a blog post. While the external testfile.php is included into the content, what I am after is a way of targeting parameters.
So for example, if the page held 4 images, I could specify in a parameter how many I would want to display. The editor could then add the following code [phpinclude file='testfile' images=2]. This would then display only 2 images instead of the default 4 images.
Does anyone know of a way to do this with the code I've shown above or point me in the right direction?
If you just add that parameter to your shortcode:
<?php
function PHP_Include($params - array()) {
extract(shortcode_atts(array(
'file' => 'default',
'images' => 4
), $params));
ob_start();
include(get_stylesheet_directory() . "/inc/$file.php");
return ob_get_clean();
}
add_shortcode('phpinclude', 'PHP_Include');
?>
then the $images variable containing the number of images to display is available to the code of your included PHP file, which can act upon it accordingly.
I am trying to modify a script that was developed to import article records from a Joomla (1.5.x) database into a Wordpress 3.2.1 table for posts. It is a script that migrates content from Joomla to Wordpress.
The issue I had with the script is that it did not maintain the unique identifier ('id' in Joomla, and 'ID' in Wordpress). Based on my understanding, this makes it a lot more complicated (much more work) to deal with redirecting all the Joomla permalinks over to the new (and obviously different) Wordpress permalinks. If the ID was the same in WP as it was in Joomla then some basic rewrite rules in htaccess would be enough to perform the redirections.
So I want to see if I can modify the code to force the ID rather than it being generated in consecutive order as records are inserted into the table.
The script I am modifying is available here: http://wordpress.org/extend/plugins/joomla-to-wordpress-migrator/
The file in question is called: joomla2wp-mig.php
The array is being created at around line 1049 and 1081.
At line 1049 it is:
$wp_posts[] = array(
'ID' => $R->id, //I ADDED THIS IN
'post_author' => $user_id,
'post_category' => array($wp_cat_id),
'post_content' => $post_content,
'post_date' => $R->created,
'post_date_gmt' => $R->created,
'post_modified' => $R->modified,
'post_modified_gmt' => $R->modified,
'post_title' => $R->title,
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_name' => $R->alias,
'tags_input' => $R->metakey,
'post_type' => 'post'
);
And at line 1081 it is:
$array = array(
"ID" => $item['ID'], //I ADDED THIS IN
"post_author" => $user_id,
"post_parent" => intval($wp_cat_id),
"post_content" => $item['post_content'],
"post_date" => $item['post_date'],
"post_date_gmt" => $item['post_date_gmt'],
"post_modified" => $item['post_modified'],
"post_modified_gmt" => $item['post_modified_gmt'],
"post_title" => $item['post_title'],
"post_status" => $item['post_status'],
"comment_status" => $item['comment_status'],
"ping_status" => $item['ping_status'],
"post_name" => $item['post_name'],
"post_type" => $item['post_type']
);
I have commented the ID line which I have added into the top of each of these bits of array code.
The INSERT command is being implimented around line 1097
The INSERT command is put together like this:
$insert_sql = "INSERT INTO " . $j2wp_wp_tb_prefix . "posts" . " set ";
$inserted = 0;
foreach ($array as $k => $v)
{
if($k AND $v)
{
if($inserted > 0)
$insert_sql .= ",";
$insert_sql .= " ".$k." = '".mysql_escape_string(str_replace("`","",$v))."'";
++$inserted;
}
}
$sql_query[] = $insert_sql;
}
It uses the MYSQL function INSERT INTO... SET (as opposed to INSERT INTO... VALUE)
The challenge I have is this:
The array did not include the ID, so I have added this in.
Having made this change, when I run the script it will appear (at the Wordpress UI end) to run fine, but no records are inserted, even though it says it was successful.
I found I could get around that issue by setting up a fresh wp_posts table with X number of blank records. Let's say I am importing 100 articles, then I would put 100 records into the table, and they would have ID 1 to 100. When I run my modified code it will happily update and populate these existing records. What I don't understand is why it will not create new records when I force the unique identifier (ID) to what I want it as.
I am wondering if I need to use the INSERT INTO... VALUE command instead of INSERT INTO... SET
I was going to test that out, but to be honest I am not a programmer and am just winging it as I go along. So I had not idea how to rewrite the PHP in order to impliment the structure required for the VALUE command in place of SET.
Any help or suggestions would be greatly appreciate.
I gather I have to rewrite the last bit of code I provded above.
There is some discussion on this matter at the wordpress support forums. One user (spiff06) very kindly helped troubleshoot the issue with me. We came unstuck around getting the code to insert new records with a forced identifier, and I went with what we referred to as the "messy" option (which is the method I mentioned above... setting up a table with the required number of blank records).
Even though I've used that "messy" method for my own site, it is my wish to make this process work cleanly for other users who are on Joomla 1.5.x and are switching to WP instead of upgrading to a newer Joomla release (which is a big process, so many are just jumping ot WP, like me).
With much thanks...
Jonathan
You can try the following:
Change the structure of the imported mysql table (post#wordpress). Change the id field so it is not anymore an autoincrement field. Let it being just an integer field.
Import the values. This way you can put any values in the field ID without limitations at all.
After the importation change again the structure of the table to set the ID field to be again an autoincrement field.
I never found a truly automated / scripted way of doing this. I ended up doing a workaround:
For now I've imported all my posts the "messy" way, by prepopulating the table.
THE WORKSROUND METHOD
Prepopulate the wp_posts table in the WP database with as many records as you require (look in Joomla to see how many records you have). I had 398, so I added 398 records to wp_posts.
HOW? I did it by exporting the emtpy wp_posts table to a .csv file. I then opened this in Excel (Numbers, or OpenOffice would also do). In the spreadsheet application it was easy to autofill 1 to 398 in the ID column.
I then reimported that .csv file into wp_posts. This gave me a wp_posts with 398 record rows with 1 to 398 in the ID field.
I then ran version 1.5.4 of Mambo/Joomla to WordPress migrator, which can be installed from within WordPress.
End result?
All posts have the same ID as the original Joomla articles.
I'm wondering if there is a technique or function to do this or if I'm just going to have to have a ton of IF statements/arrays here-
I have a page called products.php and a few different filters I add in the query string.
All the filters (if chosen) could look like this:
products.php?cat=chairs&type=pine&search=Vienna+Range
Is there a simple way to build the query for use again?
I'm going to have buttons/a search box to change the filters I'm using, so will I have to build the URL and query up again for each filter?
For example I'd like to have:
Tables
Beds
Chairs
but also build the query so that it remembers my search term, wood type and range; so clicking on "Tables" would take me to
products.php?cat=chairs&type=pine&search=Vienna+Range.
You can write something like:
<?php
$params = array(
'cat' => 'chair',
'type' => 'pine',
'search' => 'Vienna Range',
);
print_r(http_build_query($params) . PHP_EOL);
You'll get this:
cat=chair&type=pine&search=Vienna+Range