Preselect a connection type based on query string?
-
Would it be possible to click on a link and have a connection preselected based on a query string variable?
So let’s say I have a post type projects and a post type proofs. If I wanted to put an “Add New Proof” button on a projects administration page, they’d go to something like https://example.com/wp-admin/post-new.php?post_type=proof&p2p_connection_name=project_proof&p2p_to=57
where 57 is the result of get_the_ID()?
I’m guessing I can hack this together with some hideous javascript, was just wondering if it was already done.
Thanks.
https://www.ads-software.com/extend/plugins/posts-to-posts/
edit: I’ve noticed a div with a class of p2p-box has a data-p2p_type attribute of project_proof, and a div within the p2p-results table has a data-item-id attribute of 57 (the p2p_to I referenced to) so this can most definitely be done by invoking the click() method of that div. Just gotta go ahead and do it. If anyone has already done this, feel free to share, otherwise I’ll do it and share.
edit2: pretty simple, really
the selector:
jQuery('.p2p-box[data-p2p_type="project_proof"] div[data-item-id="57"]').click()
the action: admin_head-post-new.php
find your favorite way of retrieving query string variables in javascript, throw it in a simple conditional and boom. donezo. I’ll leave this open in case anyone else finds it helpful.
edit3:
just because I’d appreciate someone posting the entire code:
in functions.php:
add_action( 'admin_head-post-new.php', 'connection_preselect', 999 ); function connection_preselect() { if ( !isset($_GET['post_type']) ) { return; } ?> <script type="text/javascript"> function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.search); if(results == null) return ""; else return decodeURIComponent(results[1].replace(/\+/g, " ")); } jQuery(window).load( function($) { // The connection_type to check for var p2p_type = getParameterByName('p2p_type'); var item_id = getParameterByName('item_id'); // If ID defined, execute if(p2p_type && item_id) { jQuery('.p2p-box[data-p2p_type="' + p2p_type + '"] div[data-item-id="' + item_id + '"]').click() } }); </script> <?php } ?>
In my template:
<a target="_blank" href="<?php echo admin_url() . 'post-new.php?post_type=proof&p2p_type=project_proof&item_id=' . get_the_ID() ?>">
- The topic ‘Preselect a connection type based on query string?’ is closed to new replies.