Solved

PHP OOP Connect function to another

Posted on 2014-02-22
4
448 Views
Last Modified: 2014-02-23
I'm not sure how to ask this question and I don't have a real world example or well written code to show you, but I will do my best.
I have a Wordpress plugin that has a Functions.php file.
Function myContact() within the functions.php file creates a table.  
Each of the tables has a specific ID.
Each <tr> within the table has a specific ID.
Like so:
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>

</table>";
}

Open in new window


I don't want to mess with the plugin core functions, cause if there is an update, I will loose the work. As you know.
So, I want to learn how to attach a custom function to another function. I want to attach my function to the table in the function above.  Like so:
My Custom Function.PHP file
function myCustomFunction() {
echo '<tr id='field_three'>
<td>EMAIL</td><td>CELL PHONE</>
</tr>
<tr id='field_four'>
<td>BIRTHDAY</td><td>NICK NAME</>
</tr>
}
add_action( 'myCustomFunction', myContact' ); INSERT AFTER tr id='field_two'

Open in new window


I want to insert my function after a specific <tr id> in the original function.
Does that make sense???
0
Comment
Question by:rgranlund
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
I don't think that is going to work. Usually the function you are overriding should be written to support overridding - something like
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>";
 if (function_exists('customMyContact')) {
   customMyContact();
 }
echo "
</table>";
}

Open in new window

                                 
You can now extend the function by defining your customMyContact function.

However because the pluggin generates the complete table in one function with no external calls your only option is to replace the function with your own, modify the function to call your own or use javascript to modify the table at load time.

Either of the first two options fall foul of the pluggin upgrade overwrite problem.

The javascript option will work - basically you add some javascript code that appends the required information to the table. With JQuery it would be something like this

<script type="text/javascript">
// POPULATE YOUR HTML STRING HERE WITH PHP CODE INSERTS
var html="tr id='field_three'><td>EMAIL</td><td><?php echo $cell_phone;?></></tr><tr id='field_four'><td><?php echo $dob;?></td><td><?php echo $nickname;?></></tr>";
$(function() {
  $('#table_one').append(html);
});
</script>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
OK, another question, what does the "10' and '2' signify at the end?
add_action('personal_details_list', 'custom_purchase_details', 10, 2);
0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
Comment Utility
The 10 paramter refers to the priority given to the action - the lower the number the sooner it executes. The default is 10

The last parameter refers to the number of parameters the hook accepts.

You can read more about the add_action call here
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now