Customize WordPress comment post query - database

I am trying to extend the functionality of the comments in a WordPress install. I read allusion to an elusive 'custom comment type' functionality, but could not find any information. Instead, I was thinking that I would add a custom column ' to the 'comments' database table. That's the easy part. What I have no clue how to do is to customize the comments queries for saving, updating and reading the comments to reflect the existence of the new table column. I thought there would be a filter to alter the query, but I cannot find any that would do it... Any ideas?

There isn't really a custom comment type but you can easily and effectively add columns using "comment meta" which is a table of name/value pairs associated where each name/value pair is associated with a given comment using a 'meta_key' (please don't add a column to the SQL database; that's frowned upon in the WordPress developer community.)
Let's assume you wanted to let the user add their Twitter account. This is the code that would save my Twitter account to the comment identified by $comment_ID (prefixing the meta key name with an underscore is a good idea for any meta that you maintain via custom code vs that you let users select the meta key):
update_comment_meta($comment_ID,'_twitter','mikeschinkel');
Then to load the value to display in your template you just call get_comment_meta() (the third parameter means to return a single value, not an array of values):
$twitter = get_comment_meta($comment_ID,'_twitter',true);
Of course without knowing how to hook WordPress to integrate this the above functions would not be a lot of help. There are two hooks you'll need to use, the first being wp_insert_comment which will get called when WordPress saves a comment:
add_action('wp_insert_comment','yoursite_wp_insert_comment',10,2);
function yoursite_wp_insert_comment($comment_ID,$commmentdata) {
$twitter = isset($_GET['twitter']) ? $_GET['twitter'] : false;
update_comment_meta($comment_ID,'_twitter',$twitter);
}
The second one is a bit more complicated; the one that lets you add fields and modify other aspects of the comment form. The 'comment_form_defaults' hook sets the defaults for the comment and let's you add the HTML for a Twitter field (I snagged the format for the HTML from the comment_form() function found in /wp-includes/comment-template.php on line 1511 in WP v3.0.1)
add_filter('comment_form_defaults','yoursite_comment_form_defaults');
function yoursite_comment_form_defaults($defaults) {
$email = $defaults['fields']['email'];
$label = __( 'Twitter' );
$value = isset($_GET['twitter']) ? $_GET['twitter'] : false;
$defaults['fields']['twitter'] =<<<HTML
<p class="comment-form-twitter">
<label for="twitter">{$label}</label>
<input id="twitter" name="twitter" type="text" value="{$value}" size="30" />
</p>
HTML;
return $defaults;
}
And here's what it looks like in action:
This comment form extensibility is new for WordPress 3.0 so by its nature of being new in an open-source project it's probably not going to accommodate all use-cases yet (such as there was no easy way to get a remembered value for the Twitter screen name) but hopefully you'll be able to bend it enough to you will and get what you need and in future released of WordPress the comment form API will almost certainly improve.
Hope this helps.
-Mike
P.S. In future consider posting your question on StackOverflow's sister site WordPress Answers; that's where most of the WordPress enthusiasts hang out, those who can quickly answer questions like this.

I found this useful link related to the topic:
Customizing Comments in WordPress - Functionality and Appearance

Related

SuiteCommerce Advanced - Show a custom record on the PDP

I am looking to create a feature whereby a User can download any available documents related to the item from a tab on the PDP.
So far I have created a custom record called Documentation (customrecord_documentation) containing the following fields:
Related item : custrecord_documentation_related_item
Type : custrecord_documentation_type
Document : custrecord_documentation_document
Description : custrecord_documentation_description
Related Item ID : custrecord_documentation_related_item_id
The functionality works fine on the backend of NetSuite where I can assign documents to an Inventory item. The stumbling block is trying to fetch the data to the front end of the SCA webstore.
Any help on the above would be much appreciated.
I've come at this a number of ways.
One way is to create a Suitelet that returns JSON of the document names and urls. The urls can be the real Netsuite urls or they can be the urls of your suitelet where you set up the suitelet to return the doc when accessed with action=doc&id=_docid_ query params.
Add a target <div id="relatedDocs"></div> to the item_details.tpl
In your ItemDetailsView's init_Plugins add
$.getJSON('app/site/hosting/scriptlet.nl...?action=availabledoc').
then(function(data){
var asHtml = format(data); //however you like
$("#relatedDocs").html(asHtml);
});
You can also go the whole module route. If you created a third party module DocsView then you would add DocsView as a child view to ItemDetailsView.
That's a little more involved so try the option above first to see if it fits your needs. The nice thing is you can just about ignore Backbone with this approach. You can make this a little more portable by using a service.ss instead of the suitelet. You can create your own ssp app for the function so you don't have to deal with SCAs url structure.
It's been a while, but you should be able to access the JSON data from within the related Backbone View class. From there, within the return context, output the value you're wanting to the PDP. Hopefully you're extending the original class and not overwriting / altering the core code :P.
The model associated with the PDP should hold all the JSON data you're looking for. Model.get('...') sort of syntax.
I'd recommend against Suitelets for this, as that's extra execution time, and is a bit slower.
I'm sure you know, but you need to set the documents to be available as public as well.
Hope this helps, thanks.

How to store Array for new field type extention in Bolt cms

I am working on an extension to replace the select field type with radio/check boxes in Bolt cms. My problem is how to use bolt internals to store selected values.
Ajax POST data when I press save:
day[]: Monday
day[]: Friday
So this is the same as for select fields.
I followed this tutorial: https://docs.bolt.cm/extensions/customfields
and used
public function getStorageType(){
return 'text';
}
The response of the ajax-save request for this field is Array and this is what gets into the database. Interestingly select fields does not appear in this response. I can not find the place where data of select-fields are stored in app-code.
How can I store it correctly into database?
Using getStorageType 'json-array' results in wrong database scheme which bolt is not able to solve.
You can have a look on the code here:
https://github.com/osfriese/bolt-boxselect/tree/develope
Please help. Thanks
Tobi
I found the solution - more or less.
Sadly it is hardcoded in src/Content.php.
If you want a custom fieltype wich stores array values you have to change the select case in function getValues to:
default:
if (is_array($this->values[$field])) {
$newvalue[$field] = json_encode($this->values[$field]);
}else{
$newvalue[$field] = $this->values[$field];
}
break;
And in setValues there is a $serializedFieldTypes = array(...) where you have to manual add your custom field type.
Sadly this is not practical for extentions. But I will update my github with my changed Content.php for people who want to have a look.
When I was searching for an solution I reviewed a lot of source code of bolt master branch at github. For version 2.3 the storage layer is completely rebuild and as it seems there will be no need for any changes than. So hopefully with version >=2.3 the extention will work out of the box.
Nevertheless getStorageType() just just affect database field type and bolt just accepts 'text' here.
Hope I can help some people by answering my own question.
Thanks
Tobi

Show Opportunity related Contacts in Custom Object Field

I have the next issue.
I have a custom object called 'Application', and I have this requirement:
"Show all Contacts related to an Application. Create a field on Application object, must be read only".
I solve it with apex code. 'Application' has a lookup to Opportunity, Opportunity to Account, and all my contacts have AccountId, so this way, I get all the contacts using apex code in a trigger.
But, I've been ask to change this to a Formula field in Application object.
So, my issue is next. I'm not able to get all contacts with advance formula editor, because they're not part of any object. I have no master-detail relationship.
Does any one know how can I achieve this using configuration? I should not use apex code for this req.
Thank in advance guys.
I don't think you can do it.
In formulas / merge fields syntax there's no way to go "up, up then down" (Application -> Opportunity -> Account -> down to Contacts related list). There's also nothing that would let you loop through Contacts (and display what? Ids? Names? Emails?). Roughly speaking you can only go up through dots.
You might want to explore path of "cross object workflow" rules but I imagine that when I add a new Contact to Account it should somehow "spread itself" to all related Applications? There's no straight way to fire a workflow on delete too - so you'd eventually end up with inaccurate list.
I'd say trigger was a good solution. Maybe it ws unoptimized but if it has to be in a field - tough.
There might be a fairly simple way of achieving that by embedding a visualforce page within Application page layout.
This should be doable with pure Visualforce (so technically there will be no Apex code ;))
Something as simple as
<apex:relatedList list="Contacts" subject="Application__c.Opportunity__r.AccountId" />
would be a good start (if you want your own layout and not a rel. list - you should be still able to pull it off with <apex:repeat> or <apex:pageBlockTable>.
There's one BUT here: it's not a field, just a display trick. Forget about using it in reports, mobile applications etc.
Another way - would it be acceptable to be 1 click away from these contacts? You could make a report "Account with Contacts", filter it by Id of one Account and later use "URL hacking" to change the filter depending on from which Application you'll click it. This link could be either a formula field or a real custom button/link. Technically - it's pure config, no apex & VF.
You can read more about URL hacking at Ray Dehler's excellent post and specifically about dynamic Reports here or here.

Cakephp habtm saving data

Good evening.
Mabye I am blind, but I can't find a solution to this: I have 2 models, Post and Tag with fields post_text and tag_name, where Post habtm Tag.
Now I have form, where I input post_text and couple of tag_names. And I would like to save each of these Tags, keep them unique (so if the Tag is already in DB, don't save it, just get the ID of it), then save the Post and finaly relate the Post with the Tags via posts_tags table.
Now everybody tells me: leave it on Cake, it can do all this work for you! OK, I would love to, but how should my $this->data array look like?
I am trying Tag.tag_name, Post.Tag.tag_name, Post.Tag.0.tag_name, Post.PostsTag.0.tag_name, Tag.Tag.tag_name, Post.PostsTag.Tag.0.tag_name, ...
I am trying save(), saveAll(), ... nothing works. And all the examples on the web (including Cake Book) are working with Tag IDs, not Tag names.
Is there a way (I mean $this->data array form), that I can post to $this->Post->save() or saveAll() and it will do all the magic for me?
Thank you very much.
Josh.T.
Afraid you'll have to write the saveUnique() method yourself, someone may correct me but I don't think cake's automagic goes that far.
You'll basically have to check for each tag, if it exists, get the id(s) into an array - otherwise make it and get the id(s). Then pretty much save that array as a standard habtm with the original post.
It may even be worth using someform of AutoComplete on the add form to get tags that already exist, and then only have to create the one's that don't exist and then saving them + the habtm relationship.

Cakephp Tagging - Auto Save new Tags and Tag Relations

I read a lot about tagging in CakePHP but I can't find a "clean" way to save a Post and the Tags to this post. I have all which is necessary the Post Table, Model and Controller, the Tag table, Model and Controller and the posts_tags table. I created the HABTM Associations in the Post and the Tag Model.
If I want to save a new post, I want that CakePHP automagically saves the tags associated to that post, but I can't find the right way for that. In most of the tutorials you have to use a "helper" Function (http://www.jamesfairhurst.co.uk/posts/view/full_cakephp_application_part_5 => "_parse_genres") or something like that, but I thought the deal with CakePHP is it, that this is all done by Cake once you set it up right.
So my question, is there a "clean"-Cake-way to do it, or do I have to use a helper function?
I find it very hard to believe that you didn't find a "proper" way to handle HABTM. There are many, many articles about it. I believe that Cake will save your tags if you set your data array properly. A quick search on The Bakery:
http://bakery.cakephp.org/articles/search/3/HABTM
Will reveal enough. My guess is that you're looking for this:
http://bakery.cakephp.org/articles/view/simple-tagging-behavior
(Note that there is a component which does the same thing, but model behaviour is the right way to go)

Resources